Skip to content

Commit

Permalink
Merge pull request #297 from bounswe/backend/feature/296-format-respo…
Browse files Browse the repository at this point in the history
…nse-for-POST-/recipes/{recipeId}/comments/{commentId}/upvote-endpoint-

 Format response for post /recipes/{recipe id}/comments/{comment id}/upvote endpoint
  • Loading branch information
EnesBaserr authored May 17, 2024
2 parents 3cc1cdd + 30fb14c commit f6a814b
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,23 +173,25 @@ public ResponseEntity<?> deleteUpvote(@PathVariable Integer commentId, @PathVari
}
}

@PostMapping("/recipes/{recipeId}/comments/{commentId}/upvote")
public ResponseEntity<?> upvoteComment(@PathVariable Integer recipeId, @PathVariable Integer commentId) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Authentication required.");
}
@PostMapping("/recipes/{recipeId}/comments/{commentId}/upvote")
public ResponseEntity<?> upvoteComment(@PathVariable Integer recipeId, @PathVariable Integer commentId) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || authentication.getPrincipal().equals("anonymousUser")) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(new ErrorResponse(401, "Authentication required"));
}

String username = authentication.getName();
try {
boolean success = recipeService.upvote(commentId, username);
if (success) {
return ResponseEntity.ok().body("Comment upvoted successfully.");
} else {
return ResponseEntity.badRequest().body("Failed to upvote comment: User has already upvoted.");
String username = authentication.getName();
try {
UpvoteDto upvoteDto = recipeService.upvote(commentId, username);
if(upvoteDto != null)

return ResponseEntity.ok(new SuccessResponse<>(200, upvoteDto, "Comment upvoted successfully"));
else {
return ResponseEntity.ok(new ErrorResponse(400, "Failed to upvote comment: "));
}

} catch (IllegalStateException e) {
return ResponseEntity.ok(new ErrorResponse(400, "Failed to upvote comment: " ));
}
} catch (IllegalStateException e) {
return ResponseEntity.badRequest().body("Failed to upvote comment: " + e.getMessage());
}
}
}
21 changes: 21 additions & 0 deletions backend/src/main/java/com/group1/cuisines/dto/UpvoteDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.group1.cuisines.dto;

import java.time.LocalDateTime;
import java.util.List;

import com.group1.cuisines.entities.Ingredient;
import lombok.*;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor

public class UpvoteDto {
private Integer id;
private AuthorDto author;
private Integer recipeId;
private Integer upvoteCount;
private String content;
private LocalDateTime createdAt;
}
20 changes: 18 additions & 2 deletions backend/src/main/java/com/group1/cuisines/entities/Bookmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
import jakarta.persistence.*;
import lombok.*;

@Data
@Builder
import java.util.Objects;

@Getter
@Setter
@Builder

@NoArgsConstructor
@AllArgsConstructor
@Entity
Expand All @@ -22,4 +25,17 @@ public class Bookmark {
@ManyToOne
@JoinColumn(name = "recipe_id", nullable = false)
private Recipe recipe;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Bookmark recipe = (Bookmark) o;
return Objects.equals(id, recipe.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
21 changes: 20 additions & 1 deletion backend/src/main/java/com/group1/cuisines/entities/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

import jakarta.persistence.*;
import lombok.*;
import org.apache.commons.lang3.builder.ToStringExclude;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.Objects;
import java.util.Set;

@Data
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -18,8 +21,10 @@ public class Comment {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;


@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
@ToString.Exclude
private User user;

@ManyToOne
Expand All @@ -37,4 +42,18 @@ public class Comment {
private Set<Upvote> upvotes;

private int upvoteCount;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Comment recipe = (Comment) o;
return Objects.equals(id, recipe.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;

import java.net.URL;
import java.util.ArrayList;
Expand All @@ -14,7 +11,8 @@
import java.util.Set;


@Data
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
Expand Down
8 changes: 3 additions & 5 deletions backend/src/main/java/com/group1/cuisines/entities/Dish.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.*;

import java.util.ArrayList;
import java.util.List;

@Data
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import jakarta.persistence.*;
import lombok.*;

@Data
import java.util.Objects;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -18,4 +21,16 @@ public class Ingredient {
@ManyToOne
@JoinColumn(name = "recipe_id") // This column in the Ingredient table will hold the foreign key to the Recipe
private Recipe recipe; // This is the 'recipe' field expected by the 'mappedBy' attribute
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Ingredient recipe = (Ingredient) o;
return Objects.equals(id, recipe.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
18 changes: 17 additions & 1 deletion backend/src/main/java/com/group1/cuisines/entities/Rating.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import jakarta.persistence.*;
import lombok.*;

@Data
import java.util.Objects;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -23,4 +26,17 @@ public class Rating {
private Recipe recipe;

private int ratingValue; // Assuming ratings are integer values

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Rating recipe = (Rating) o;
return Objects.equals(id, recipe.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}
}
17 changes: 16 additions & 1 deletion backend/src/main/java/com/group1/cuisines/entities/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@Data
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
Expand Down Expand Up @@ -39,6 +41,7 @@ public class Recipe {
private Date createdAt;

@ManyToOne
@ToString.Exclude
@JoinColumn(name = "user_id", nullable = false)
private User user;
@OneToMany(mappedBy = "recipe", cascade = CascadeType.ALL, orphanRemoval = true)
Expand All @@ -49,6 +52,18 @@ public class Recipe {
@CollectionTable(name = "recipe_allergens", joinColumns = @JoinColumn(name = "recipe_id"))
@Column(name = "allergen")
private List<String> allergens = new ArrayList<>();
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Recipe recipe = (Recipe) o;
return Objects.equals(id, recipe.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}

@Override
public String toString() {
Expand Down
20 changes: 19 additions & 1 deletion backend/src/main/java/com/group1/cuisines/entities/Upvote.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import jakarta.persistence.*;
import lombok.*;

@Data
import java.time.LocalDateTime;
import java.util.Objects;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
Expand All @@ -17,8 +21,22 @@ public class Upvote {
@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private User user;
private LocalDateTime createdDate = LocalDateTime.now();

@ManyToOne
@JoinColumn(name = "comment_id", nullable = false)
private Comment comment;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Upvote upvote = (Upvote) o;
return Objects.equals(id, upvote.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}

}
3 changes: 2 additions & 1 deletion backend/src/main/java/com/group1/cuisines/entities/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.Objects;
import java.util.Set;

@Data

@Builder
@NoArgsConstructor
@AllArgsConstructor
Expand Down Expand Up @@ -80,6 +80,7 @@ public boolean equals(Object obj) {
return Objects.equals(id, other.id); // Compare only IDs for equality
}


@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public CommentsDto deleteUpvote(Integer commentId, Integer recipeId, String user


@Transactional
public boolean upvote(Integer commentId, String username) {
public UpvoteDto upvote(Integer commentId, String username) {
User user = userRepository.findByUsername(username).orElse(null);
if (user == null) {
throw new IllegalStateException("User not found");
Expand All @@ -348,18 +348,31 @@ public boolean upvote(Integer commentId, String username) {

Optional<Upvote> existingUpvote = upvoteRepository.findByCommentIdAndUserId(commentId, user.getId());
if (existingUpvote.isPresent()) {
return false;
return null;
}

Upvote upvote = Upvote.builder()
.user(user)
.comment(comment)
.createdDate(LocalDateTime.now())
.build();

upvoteRepository.save(upvote);
comment.setUpvoteCount(comment.getUpvotes().size() + 1);

comment.setUpvoteCount(comment.getUpvoteCount()+1);
comment.getUpvotes().add(upvote);
commentRepository.save(comment);
return true;
return new UpvoteDto().builder()
.id(upvote.getId())
.author(new AuthorDto(comment.getUser().getId(), comment.getUser().getFirstName()+ " " + comment.getUser().getLastName(),
comment.getUser().getUsername(), comment.getUser().getFollowers().size(),
comment.getUser().getFollowing().size(), comment.getUser().getRecipeCount()))
.recipeId(comment.getRecipe().getId())
.upvoteCount(comment.getUpvoteCount())
.content(comment.getText())
.createdAt(upvote.getCreatedDate())
.build();

}

}

0 comments on commit f6a814b

Please sign in to comment.