Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email Clean Architecture + Tests #41

Merged
merged 9 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package callhub.connect.data_access;

import callhub.connect.entities.Message;
import callhub.connect.entities.Session;
import callhub.connect.entities.exceptions.NoMessagesException;
import callhub.connect.entities.exceptions.SessionNotFoundException;
import callhub.connect.use_case.email.EmailDataAccessInterface;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class EmailDataAccessObject implements EmailDataAccessInterface {
private final SessionRepository sessionRepository;

public EmailDataAccessObject(SessionRepository sessionRepository) {
this.sessionRepository = sessionRepository;
}

/**
* Retrieves the transcript of messages for a given session code.
*
* @param id The session code to identify the session.
* @return A ResponseEntity containing the transcript of messages as a String.
* @throws NullPointerException if there is no session with the id in the sessionRepository.
*/
@Override
public String getTranscript(String id) {
boolean sessionExists = sessionRepository.existsById(id);
if (!sessionExists) {
throw new SessionNotFoundException("Session not found.");
}

Session session = sessionRepository.getSessionById(id);
ArrayList<Message> messagesList = session.getMessages();

if (!(messagesList.isEmpty())) {
StringBuilder transcript = new StringBuilder();
for (Message message : messagesList) {
transcript.append(message.formattedMessage()).append("\n");
}
return transcript.toString();
}
else{
throw new NoMessagesException("No messages found.");
}
}

/**
* Retrieves the date messages were sent for a given session code, assuming all messages
* were sent on the same day as the first message.
*
* @param id The session code to identify the session.
* @return A ResponseEntity containing the date of messages as a String.
* @throws NullPointerException if there is no session with the id in the sessionRepository.
*/
@Override
public String getDate(String id) {
boolean sessionExists = sessionRepository.existsById(id);
if (!sessionExists) {
throw new SessionNotFoundException("Session not found.");
}

Session session = sessionRepository.getSessionById(id);
ArrayList<Message> messagesList = session.getMessages();

if (!(messagesList.isEmpty())) {
Message firstMessage = messagesList.get(0);
return firstMessage.getDateString();
}
else {
throw new NoMessagesException("No messages found.");
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/callhub/connect/entities/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public Session(boolean active, String code){
this.documents = new ArrayList<String>();
}

/**
* Sets the unique identifier (ID) of the session for testing purposes.
*/
public void setId(String id) {
this.id = id;
}

/**
* Gets the unique identifier (ID) associated with the session.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package callhub.connect.entities.exceptions;

public class NoMessagesException extends RuntimeException {
public NoMessagesException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package callhub.connect.entities.exceptions;

public class SessionNotFoundException extends RuntimeException {
public SessionNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package callhub.connect.interface_adapter.email;

import callhub.connect.use_case.email.EmailInputBoundary;
import callhub.connect.use_case.email.EmailInputData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/email")
public class EmailController {

@Autowired
private EmailInputBoundary emailInteractor;

@GetMapping("/transcript/{id}")
public ResponseEntity<String> getTranscript(@PathVariable String id) {
EmailInputData inputData = new EmailInputData(id);
return emailInteractor.getTranscript(inputData);
}

@GetMapping("/date/{id}")
public ResponseEntity<String> getCode(@PathVariable String id) {
EmailInputData inputData = new EmailInputData(id);
return emailInteractor.getDate(inputData);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package callhub.connect.interface_adapter.email;

import callhub.connect.use_case.email.EmailOutputBoundary;
import callhub.connect.use_case.email.EmailOutputData;
import org.springframework.stereotype.Service;

@Service
public class EmailPresenter implements EmailOutputBoundary {

@Override
public String getResponse(EmailOutputData outputData) {
return outputData.getResponseBody();
}
}
81 changes: 0 additions & 81 deletions src/main/java/callhub/connect/use_case/EmailController.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package callhub.connect.use_case.email;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.HashMap;

@Service
public interface EmailDataAccessInterface {

String getTranscript(String id);

String getDate(String id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package callhub.connect.use_case.email;

import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public interface EmailInputBoundary {
ResponseEntity<String> getTranscript(EmailInputData inputData);

ResponseEntity<String> getDate(EmailInputData inputData);
}
11 changes: 11 additions & 0 deletions src/main/java/callhub/connect/use_case/email/EmailInputData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package callhub.connect.use_case.email;

public class EmailInputData {
private final String id;
public EmailInputData(String id) {
this.id = id;
}
public String getId() {
return this.id;
}
}
53 changes: 53 additions & 0 deletions src/main/java/callhub/connect/use_case/email/EmailInteractor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package callhub.connect.use_case.email;

import callhub.connect.entities.exceptions.NoMessagesException;
import callhub.connect.entities.exceptions.SessionNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class EmailInteractor implements EmailInputBoundary {

@Autowired
final EmailDataAccessInterface emailDataAccessObject;

@Autowired
final EmailOutputBoundary emailPresenter;

public EmailInteractor(EmailDataAccessInterface emailDataAccessObject, EmailOutputBoundary emailPresenter) {
this.emailDataAccessObject = emailDataAccessObject;
this.emailPresenter = emailPresenter;
}

@Override
public ResponseEntity<String> getTranscript(EmailInputData inputData) {
HttpHeaders headers = new HttpHeaders();
try {
String transcript = emailDataAccessObject.getTranscript(inputData.getId());
EmailOutputData outputData = new EmailOutputData(transcript);
return new ResponseEntity<>(emailPresenter.getResponse(outputData), headers, HttpStatus.OK);
} catch (SessionNotFoundException e) {
return new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND);
} catch (NoMessagesException e) {
return new ResponseEntity<>("This session has no messages.", headers, HttpStatus.NOT_FOUND);
}
}


@Override
public ResponseEntity<String> getDate(EmailInputData inputData) {
HttpHeaders headers = new HttpHeaders();
try {
String date = emailDataAccessObject.getDate(inputData.getId());
EmailOutputData outputData = new EmailOutputData(date);
return new ResponseEntity<>(emailPresenter.getResponse(outputData), headers, HttpStatus.OK);
} catch (SessionNotFoundException e) {
return new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND);
} catch (NoMessagesException e) {
return new ResponseEntity<>("This session has no messages.", headers, HttpStatus.NOT_FOUND);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package callhub.connect.use_case.email;

import org.springframework.stereotype.Service;

@Service
public interface EmailOutputBoundary {
String getResponse(EmailOutputData outputData);
}
11 changes: 11 additions & 0 deletions src/main/java/callhub/connect/use_case/email/EmailOutputData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package callhub.connect.use_case.email;

public class EmailOutputData {
private final String responseBody;
public EmailOutputData(String responseBody) {
this.responseBody = responseBody;
}
public String getResponseBody() {
return this.responseBody;
}
}
Loading
Loading