Skip to content

Commit

Permalink
Merge pull request #320 from COSC-499-W2023/BlurringS3OnProd
Browse files Browse the repository at this point in the history
Final changes, Notification and Comments enabled
  • Loading branch information
Abdulhameed23 authored Apr 7, 2024
2 parents 2ad135d + 2075e14 commit 96c1225
Show file tree
Hide file tree
Showing 28 changed files with 709 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.exzbt.business.comment;

import com.exzbt.business.comment.mappers.CommentDTO;
import com.exzbt.business.comment.mappers.CommentRequest;
import com.exzbt.business.notification.NotificationService;
import com.exzbt.business.request.RequestService;
import com.exzbt.business.request.mappers.RequestDetailsDTO;
import com.exzbt.transaction.comments.api.CommentsTransactions;
import com.exzbt.transaction.comments.impl.Comment;
import com.exzbt.transaction.notifications.api.NotificationTransactions;
import com.exzbt.transaction.notifications.impl.Notification;
import com.exzbt.transaction.request.impl.Request;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

@Service
@AllArgsConstructor
public class CommentService {
@Autowired
private CommentsTransactions commentsTransactions;

@Autowired
private NotificationService notificationService;

@Autowired
private RequestService requestService;

public List<CommentDTO> getCommentsByRequestId(String requestId) {
List<Comment> comments = commentsTransactions.findByRequestId(requestId);
return comments.stream()
.map(comment -> new CommentDTO().convertDTO(comment)).toList();
}

public void createComment(CommentRequest commentRequest) {
RequestDetailsDTO requestDetails = requestService.getRequestByRequestId(commentRequest.getRequestId());

commentsTransactions.save(commentRequest.convertFromDTO());

String assigneeId = Objects.equals(commentRequest.getCreatorId(), requestDetails.getAssigneeId())?
requestDetails.getCreatorId() : requestDetails.getAssigneeId();

notificationService.sendNotification(requestDetails.getTitle() + " - has a new comment!",
assigneeId , commentRequest.getCreatorId(), new Date());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.exzbt.business.comment.mappers;

import com.exzbt.transaction.comments.impl.Comment;
import com.exzbt.transaction.notifications.impl.Notification;
import jakarta.persistence.Column;
import lombok.*;

import java.util.Date;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@ToString
@EqualsAndHashCode
@Setter
public class CommentDTO {
private String commentId;
private String sender;
private String requestId;
private String content;
private Date created;

public CommentDTO convertDTO(Comment comment) {
this.setCommentId(comment.getCommentId());
this.setSender(comment.getSender());
this.setRequestId(comment.getRequestId());
this.setContent(comment.getContent());
this.setCreated(comment.getCreated());
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.exzbt.business.comment.mappers;

import com.exzbt.transaction.comments.impl.Comment;
import lombok.*;

import java.util.Date;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@ToString
@EqualsAndHashCode
@Setter
public class CommentRequest {
private String sender;
private String requestId;
private String creatorId;
private String content;
private Date created;

public Comment convertFromDTO() {
Comment comment = new Comment();
comment.setContent(this.getContent());
comment.setCreated(new Date());
comment.setSender(this.getSender());
comment.setRequestId(this.getRequestId());

return comment;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public Notification convertFromDTO() {
Notification notification = new Notification();
notification.setContent(this.getContent());
notification.setCreated(new Date());
notification.setCreatorId(this.getContent());
notification.setCreatorId(this.getCreatorId());
notification.setAssigneeId(this.getAssigneeId());
notification.setViewed(Boolean.FALSE);
return notification;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public RequestDetailsDTO createRequest(RequestCreationDTORequest creationDTORequ
UserDetailsDTO assignedUser = userService.findUserByEmail(creationDTORequest.getAssigneeEmail());
creationDTORequest.setAssigneeId(assignedUser.getUserId());
Request request = requestActions.save(creationDTORequest.convertFromDTO());
notificationService.sendNotification("New Request Assigned",
notificationService.sendNotification("New Request Assigned - " + request.getTitle(),
request.getAssigneeId(), request.getCreatorId(), new Date());
return new RequestDetailsDTO().convertDTO(request);
}
Expand All @@ -94,7 +94,7 @@ public RequestDetailsDTO saveChanges(String requestId, RequestDetailsRequest upd
request.setAssigneeId(updateRequest.getAssigneeId());
request.setExpiration(updateRequest.getExpiration());

notificationService.sendNotification("Request Updated",
notificationService.sendNotification("Request Updated - " + request.getTitle(),
request.getAssigneeId(), request.getCreatorId(), new Date());
return new RequestDetailsDTO().convertDTO(requestActions.save(request));
}
Expand All @@ -107,7 +107,7 @@ public void deleteRequestByRequestId(String requestId) {
Request request = requestActions.findById(requestId)
.orElseThrow(RuntimeException::new);

notificationService.sendNotification("Request Deleted",
notificationService.sendNotification("Request Deleted - " + request.getTitle(),
request.getAssigneeId(), request.getCreatorId(), new Date());

requestActions.deleteById(requestId);
Expand Down Expand Up @@ -145,8 +145,8 @@ public void uploadVideoOnSubmit(String requestId, MultipartFile file, String use
.orElseThrow(RuntimeException::new);
request.setSubmitted(Boolean.TRUE);

notificationService.sendNotification("Video Submitted to Request",
request.getAssigneeId(), request.getCreatorId(), new Date());
notificationService.sendNotification("Video Submitted to Request - " + request.getTitle(),
request.getCreatorId(), request.getAssigneeId(), new Date());
requestActions.save(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,18 @@ public UserDetailsDTO saveChanges(String id, UserDetailRequest updateRequest) {
AppUser user = userTransaction.findById(id)
.orElseThrow(() -> new UsernameNotFoundException(USER_NOT_FOUND));

if(Objects.isNull(updateRequest) || Objects.equals(user, updateRequest.convertFromDTO())) {
if(Objects.isNull(updateRequest)) {
//TODO: exception throw
return null;
}

AppUser updatedUser = updateRequest.convertFromDTO();
return new UserDetailsDTO().convertDTO(userTransaction.save(updatedUser));
user.setFirstName(updateRequest.getFirstName());
user.setLastName(updateRequest.getLastName());
if (Objects.nonNull(updateRequest.getPassword()) && !updateRequest.getPassword().isEmpty()) {
user.setPassword(updateRequest.getPassword());
}

return new UserDetailsDTO().convertDTO(userTransaction.save(user));
}

public void deleteUserById(String userId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.exzbt.transaction.comments.api;

import com.exzbt.transaction.comments.impl.Comment;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@Transactional
public interface CommentsTransactions extends JpaRepository<Comment, String> {
List<Comment> findByRequestId(String requestId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.exzbt.transaction.comments.impl;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.GenericGenerator;

import java.util.Date;

@Entity
@Table(name = "Comment")
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@EqualsAndHashCode
public class Comment {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(
name = "UUID",
strategy = "org.hibernate.id.UUIDGenerator"
)
private String commentId;

@Column(nullable = false)
private String sender;

@Column(nullable = false)
private String requestId;

@Column
private String content;

@Column(nullable = false)
private Date created;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
@Transactional
public interface NotificationTransactions extends JpaRepository<Notification, String> {
List<Notification> findByAssigneeId(String assigneeId);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.exzbt.widget.comment;

import com.exzbt.business.comment.CommentService;
import com.exzbt.business.comment.mappers.CommentDTO;
import com.exzbt.business.comment.mappers.CommentRequest;
import com.exzbt.business.notification.NotificationService;
import com.exzbt.business.notification.mappers.NotificationDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/v1/comments")
public class CommentCRUD {
@Autowired
private CommentService commentService;

@GetMapping("{requestId}")
public List<CommentDTO> getComments(@PathVariable("requestId") String requestId) {
return commentService.getCommentsByRequestId(requestId);
}

@PostMapping("create/{requestId}")
public void updateNotifications(@RequestBody CommentRequest request) {
commentService.createComment(request);
}
}
76 changes: 72 additions & 4 deletions app/client/src/components/Comments/Comments.jsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,80 @@
import React from 'react'
import React, { useEffect, useState } from 'react';
import Box from '@mui/material/Box';
import "./Comments.scss";
import { useAuth } from '../../context/authContext';
import { getCommentsById, createNewComment } from '../../services/ClientAPI';

function Comments() {
const { currentUser, currentRequest } = useAuth();
const [comments, setComments] = useState([]);
const [commentContent, setContent] = useState("");

const getComments = () => {
getCommentsById(currentRequest.requestId)
.then((resp) => {
let sorted = resp.data.sort((a,b) => a.created - b.created);
setComments(sorted);
})
.catch((err) => {});
};

const handleSubmitComment = () => {
const commentData = new FormData();
commentData.append("requestId", currentRequest.requestId);
commentData.append("creatorId", currentUser.userId);
commentData.append("content", commentContent);
commentData.append("sender", currentUser.lastname + ", " + currentUser.firstname);

createNewComment(commentData, currentRequest.requestId)
.then((resp) => {
getCommentsById(currentRequest.requestId).then((resp) => {
let sorted = resp.data.sort((a,b) => a.created - b.created);
setComments(sorted);
setContent("");
})
})
.catch((err) => {});
};

useEffect(() => {
getComments();
}, []);

const CommentList = (
<Box id="box" sx={{ width: 350 }} role="presentation">
<div>
{comments.length === 0 ? (
<div className="noNotifs">No recent comments!</div>
) : (
<div id="commentsContainer">
{comments.map((comment) => (
<div className="notificationBox commentsBox" key={comment.commentId}>
<div className="commentContent">
<p>
{comment.content}
</p>
<div className='commentInfo'>
<span>From {comment.sender}</span>
<span>{new Date(comment.created).toLocaleString()}</span>
</div>
</div>
</div>
))
}
</div>
)}
</div>
</Box>
);

return (
<div className='comments-main'>
<div className='comments-flexbox'>
<h3 data-testid="header">Comments</h3>
<textarea data-testid="textArea"/>
<button data-testid="submitButton">Submit</button>
<div className='commentEntries'>
{CommentList}
</div>
<textarea onChange={(e) => setContent(e.target.value)} value={commentContent} maxlength="200" data-testid="textArea"/>
<button onClick={handleSubmitComment} data-testid="submitButton">Submit</button>
</div>

</div>
Expand Down
Loading

0 comments on commit 96c1225

Please sign in to comment.