STG-7 / CSABlogging

MIT License
0 stars 1 forks source link

2015 FRQ Issue - Shaurya Goel #9

Open STG-7 opened 8 months ago

STG-7 commented 8 months ago

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.

@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();
}

}

- 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.
rohinsood commented 8 months ago

FRQ 1:

Code (.95/1)

FRQ 2:

Code (.95/1)

FRQ 3:

Code (.95/1.0)

FRQ 4:

Code (.95/1.0)