In the sendMessage method, a new Chat object is created and saved using ChatJpaRepository. This method is a POST mapping that expects parameters for email, message, fromEmail, personId, and fromPersonId.
@PostMapping("create")
public String sendMessage(@RequestParam String email, @RequestParam String message, @RequestParam String fromEmail,
@RequestParam long personId, @RequestParam long fromPersonId) {
Chat chat = new Chat(email, message, fromEmail, personId, fromPersonId);
Chat savedChat = chatJpaRepository.save(chat);
// store this message somewhere and deliver to user
return "Message Sent Successfully. ID: " + savedChat.getId();
}
Here, I am creating a new Chat object using the provided parameters.
Then, I save this Chat object using the chatJpaRepository.save(chat) method.
Finally, I return a message indicating the successful sending of the message, along with the ID of the saved Chat object.
@Entity (name = "chat")
public class Chat {
public Chat() {}
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@NotNull
@Column(name = "message")
private String message;
@NotNull
@Column(name = "email")
private String email;
@NotNull
@Column(name = "person_id")
private Long personId;
@NotNull
@Column(name = "from_person_id")
private Long fromPersonId;
@NotNull
@Column(name = "from_email")
private String fromEmail;
@NotNull
@Column(name = "read_flag")
private boolean readFlag = false;
@Column(name = "date_sent")
private Date dateSent;
@Column(name = "date_read")
private Date dateRead;
/*@ManyToOne
@JoinColumn(name = "id")
private Person person;*/
public Chat (String email, String message, String fromEmail, long personId, long fromPersonId){
this.email = email;
this.message = message;
this.fromEmail = fromEmail;
this.readFlag = false;
this.personId = personId;
this.fromPersonId = fromPersonId;
this.dateSent = new Date(System.currentTimeMillis());
}
public Long getId() {
return id;
}
public Long getPersonId() {
return personId;
}
public Long getFromPersonId() {
return fromPersonId;
}
public String getMessage() {
return message;
}
public String getEmail() {
return email;
}
public String getFromEmail() {
return fromEmail;
}
public boolean isReadFlag(){
return readFlag;
}
public void setReadFlag(boolean readFlag) {
this.readFlag = readFlag;
}
public Date getDateSent() {
return dateSent;
}
public void setDateSent(Date dateSent) {
this.dateSent = dateSent;
}
public Date getDateRead() {
return dateRead;
}
public void setDateRead(Date dateRead) {
this.dateRead = dateRead;
}
}
- The Chat class represents an individual chat message in the system.
- Instances of Chat objects are created when new messages are sent, with each instance representing a single chat message.
- The class provides methods to access and modify attributes of Chat objects, such as getting the message content, email addresses, read status, and dates.
# FRQ 3 (Arrays)
### [FRQ 3 Link](https://stg-7.github.io/CSABlogging//2024/02/22/2015FRQ3_IPYNB_2_.html)
## Association
### Reading Message History in ChatApiController.java
In the readAllMessages method, I retrieve all chat messages between two specific email addresses and return them as a JSON array.
@GetMapping("history")
public String readAllMessages(@RequestParam String email, @RequestParam String fromEmail) throws JsonProcessingException {
List histMessages = chatJpaRepository.findByEmailAndFromEmailOrFromEmailAndEmailOrderByDateSent(email,
fromEmail);
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(histMessages);
// store this message somewhere and deliver to user
return json;
}
- Here, I use chatJpaRepository.findByEmailAndFromEmailOrFromEmailAndEmailOrderByDateSent to fetch all messages between the specified email and fromEmail.
- The retrieved list of Chat objects is then converted to a JSON string using ObjectMapper, and returned to the user.
# FRQ 4 (Interfaces)
### [FRQ 4 Link](https://stg-7.github.io/CSABlogging//2024/02/22/2015FRQ4_IPYNB_2_.html)
## Association
### findByEmail in ChatJpaRepository.java
List findByEmail(String email);
This method returns a List<Chat> of all chat messages where the email matches the provided parameter. The returned list acts as a 1D array of Chat objects containing messages for a specific email.
### findByEmailAndFromEmailAndReadFlagOrderByDateSent
List findByEmailAndFromEmailAndReadFlagOrderByDateSent(String email, String fromEmail, boolean readFlag);
- Similar to the previous method, this returns a List<Chat> of unread messages between two emails, sorted by date sent. Again, the returned list serves as a 1D array of Chat objects.
Well-Organized Code: The code is structured logically for readability and maintainability.
Modular Approach: The use of functions like arraySum, rowSums, and isDiverse promotes code organization and reuse.
User-Friendly Input: Inclusion of a Scanner for 2D array input enhances user experience. The displayed input grid adds clarity.
Associations (.90/1)
Your method displays how the API requests can return data of variable length
FRQ 2:
Code (.95/1)
Clear Class Structure: Neatly encapsulates hidden word and hint generation in the HiddenWord class.
Robust Input Handling: Ensures user provides a guess of the same length as the hidden word, offering informative feedback.
Informative Comments: Well-documented with comments explaining each method and section for enhanced readability and reference.
Associations (.90/1)
Your code displays how getters and setters via annotations create the blueprints of a class
The usage of this code in your project shows your strong understanding of java topics surrounding object oriented programming languages
FRQ 3:
Code (.95/1.0)
User-Friendly Interaction: The implementation guides users through inputting sparse array dimensions, values, and performing operations like retrieving values and removing columns.
Logical Entry Handling: The addEntry method logically handles sparse array entries, ensuring that zero values are not added, and it dynamically updates numRows and numCols based on the entries.
Readability: The code structure is clear and easy to follow, with concise methods like getValueAt and removeColumn. Meaningful variable names enhance overall code readability.
Associations (.90/1.0)
Storing data within a json object displays how iterable objects can be used
FRQ 4:
Code (.95/1.0)
Clear Interface Design: The NumberGroup interface's single method simplifies representation and implementation.
Effective Range Class: Range class efficiently represents integer ranges with a concise constructor and contains method.
Efficient Collection Handling: MultipleGroups class adeptly manages a collection of NumberGroup objects, accurately checking number containment.
Associations (.90/1.0)
Those methods, without implementations, shows how you understand and can relate the abstractedness of interfaces and how they must be implemented on a different layer of run time
FRQ 1 (FRQ 1 (1D & 2D Arraylists)
FRQ 1 Link
Association
Sending a Message in ChatApiController.java
In the sendMessage method, a new Chat object is created and saved using ChatJpaRepository. This method is a POST mapping that expects parameters for email, message, fromEmail, personId, and fromPersonId.
Finally, I return a message indicating the successful sending of the message, along with the ID of the saved Chat object.
FRQ 2 (Classes)
FRQ 2 Link
Association
Chat.java
}
@GetMapping("history") public String readAllMessages(@RequestParam String email, @RequestParam String fromEmail) throws JsonProcessingException { List histMessages = chatJpaRepository.findByEmailAndFromEmailOrFromEmailAndEmailOrderByDateSent(email,
fromEmail);
}
List findByEmail(String email);
List findByEmailAndFromEmailAndReadFlagOrderByDateSent(String email, String fromEmail, boolean readFlag);