From c97f02fb32c432a469cd87261887020a9af8236d Mon Sep 17 00:00:00 2001 From: adalinmik Date: Sun, 26 May 2024 21:48:49 +0200 Subject: [PATCH] RecommendationService update --- .../RecommendationController.java | 6 +- .../recommendation/RecommendationService.java | 78 +++++++++++-------- .../recommendation/RecommendationSimple.java | 9 ++- src/main/resources/dozer.xml | 4 - 4 files changed, 54 insertions(+), 43 deletions(-) diff --git a/src/main/java/hu/tilos/radio/backend/recommendation/RecommendationController.java b/src/main/java/hu/tilos/radio/backend/recommendation/RecommendationController.java index 3685506..6cd893b 100644 --- a/src/main/java/hu/tilos/radio/backend/recommendation/RecommendationController.java +++ b/src/main/java/hu/tilos/radio/backend/recommendation/RecommendationController.java @@ -34,18 +34,18 @@ public RecommendationData get(@PathVariable String id) { @PreAuthorize("hasRole('ROLE_AUTHOR')") @RequestMapping(value = "/api/v1/recommendation", method = RequestMethod.POST) public CreateResponse create(@RequestBody @Validated RecommendationToSave recommendation) { - return recommendationService.create(recommendation, userService.getCurrentUser()); + return recommendationService.create(recommendation, userService.getUser(userService.getCurrentUser().getUsername())); } @PreAuthorize("hasRole('ROLE_AUTHOR')") @RequestMapping(value = "/api/v1/recommendation/{id}", method = RequestMethod.PUT) public UpdateResponse update(@PathVariable String id, @RequestBody @Validated RecommendationToSave recommendation) { - return recommendationService.update(id, recommendation); + return recommendationService.update(id, recommendation, userService.getUser(userService.getCurrentUser().getUsername())); } @PreAuthorize("hasRole('ROLE_AUTHOR')") @RequestMapping(value = "/api/v1/recommendation/{id}", method = RequestMethod.DELETE) public OkResponse delete(@PathVariable String id) { - return recommendationService.delete(id); + return recommendationService.delete(id, userService.getUser(userService.getCurrentUser().getUsername())); } } diff --git a/src/main/java/hu/tilos/radio/backend/recommendation/RecommendationService.java b/src/main/java/hu/tilos/radio/backend/recommendation/RecommendationService.java index a092f05..dafcbe6 100644 --- a/src/main/java/hu/tilos/radio/backend/recommendation/RecommendationService.java +++ b/src/main/java/hu/tilos/radio/backend/recommendation/RecommendationService.java @@ -1,16 +1,19 @@ package hu.tilos.radio.backend.recommendation; import com.mongodb.*; +import hu.tilos.radio.backend.auth.AuthorRepository; import hu.tilos.radio.backend.auth.UserInfo; import hu.tilos.radio.backend.data.Author; import hu.tilos.radio.backend.data.error.NotFoundException; import hu.tilos.radio.backend.data.response.CreateResponse; import hu.tilos.radio.backend.data.response.OkResponse; import hu.tilos.radio.backend.data.response.UpdateResponse; +import hu.tilos.radio.backend.auth.Role; import org.bson.types.ObjectId; import org.dozer.DozerBeanMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.inject.Inject; @@ -27,6 +30,8 @@ public class RecommendationService { private DB db; private static final String COLLECTION = "recommendation"; + @Autowired + private AuthorRepository authorRepository; public List list(String type) { BasicDBObject criteria = new BasicDBObject(); @@ -36,7 +41,13 @@ public List list(String type) { criteria.put("type", RecommendationType.valueOf(type).ordinal()); } - DBCursor selectedRecommendations = db.getCollection(COLLECTION).find(criteria).sort(new BasicDBObject("created", -1)); + DBCursor selectedRecommendations = db.getCollection(COLLECTION).find(criteria); + + if (!"all".equals(type) && RecommendationType.EVENT == RecommendationType.valueOf(type)){ + selectedRecommendations.sort(new BasicDBObject("date", -1)); + } else { + selectedRecommendations.sort(new BasicDBObject("created", -1)); + } List mappedRecommendations = new ArrayList<>(); @@ -47,14 +58,14 @@ public List list(String type) { mappedRecommendations.add(mapper.map(selectedRecommendation, RecommendationSimple.class)); } - /*Collections.sort(mappedRecommendations, new Comparator() { - @Override - public int compare(RecommendationSimple s1, RecommendationSimple s2) { - return s1.getTitle().toLowerCase().compareTo(s2.getTitle().toLowerCase()); + for (RecommendationSimple recommendation : mappedRecommendations) { + if (recommendation.getAuthor() != null) { + Author author = authorRepository.findById(recommendation.getAuthor().getId()).get(); + recommendation.setAuthor(author); } - });*/ - return mappedRecommendations; + } + return mappedRecommendations; } public RecommendationData get(String id) { @@ -66,60 +77,63 @@ public RecommendationData get(String id) { RecommendationData recommendation = mapper.map(recommendationObject, RecommendationData.class); + if (recommendation.getAuthor() != null) { + Author author = authorRepository.findById(recommendation.getAuthor().getId()).get(); + recommendation.setAuthor(author); + } + return recommendation; } public CreateResponse create(RecommendationToSave recommendationToSave, UserInfo userInfo) { - Author author; + Author author = null; - /*author = recommendationToSave.getAuthor();*/ - - /*if (recommendationToSave.getAuthor() != null) { - author = recommendationToSave.getAuthor(); - } else if (userInfo.getAuthor() != null) { + if (userInfo.getAuthor() != null) { author = userInfo.getAuthor(); - } else { - throw new RuntimeException("Only authors can create recommendation, or you can specify the author in the request."); - }*/ + } DBObject recommendation = mapper.map(recommendationToSave, BasicDBObject.class); - /*BasicDBObject authorObj = new BasicDBObject(); - authorObj.put("alias", author.getAlias()); - authorObj.put("name", author.getName()); - authorObj.put("ref", new DBRef("author", author.getId())); - recommendation.put("author", authorObj);*/ - recommendation.put("created", new Date()); + recommendation.put("author", author != null ? new DBRef("author", new ObjectId(author.getId())) : null); db.getCollection(COLLECTION).insert(recommendation); return new CreateResponse(((ObjectId) recommendation.get("_id")).toHexString()); } - public UpdateResponse update(String id, RecommendationToSave recommendationToSave) { - /*Author author = recommendationToSave.getAuthor();*/ + public UpdateResponse update(String id, RecommendationToSave recommendationToSave, UserInfo userInfo) { - DBObject recommendationObject = mapper.map(recommendationToSave, BasicDBObject.class); + // Is owner + RecommendationData recommendation = get(id); + if (recommendation.getAuthor() != null) { + Author author = authorRepository.findById(recommendation.getAuthor().getId()).get(); + if (!author.getId().equals(recommendation.getAuthor().getId())) { + throw new IllegalArgumentException("You are not the owner of this recommendation"); + } + } - /*BasicDBObject authorObj = new BasicDBObject(); - authorObj.put("alias", author.getAlias()); - authorObj.put("name", author.getName()); - authorObj.put("ref", new DBRef("author", author.getId()));*/ - /*recommendationObject.put("author", authorObj);*/ + DBObject recommendationObject = mapper.map(recommendationToSave, BasicDBObject.class); - /*DBObject recommendationObject = mapper.map(recommendationToSave, BasicDBObject.class);*/ db.getCollection(COLLECTION).update(new BasicDBObject("_id", new ObjectId(id)), recommendationObject); return new UpdateResponse(true); } - public OkResponse delete(String id) { + public OkResponse delete(String id, UserInfo userInfo) { RecommendationData recommendation = get(id); if (recommendation == null) { throw new IllegalArgumentException("Recommendation not found: " + id); } + // Is owner + if (userInfo.getRole() != Role.ADMIN) { + Author author = authorRepository.findById(recommendation.getAuthor().getId()).get(); + if (!author.getId().equals(recommendation.getAuthor().getId())) { + throw new IllegalArgumentException("You are not the owner of this recommendation"); + } + } + db.getCollection(COLLECTION).remove(new BasicDBObject("_id", new ObjectId(id))); return new OkResponse("Recommendation has been deleted"); diff --git a/src/main/java/hu/tilos/radio/backend/recommendation/RecommendationSimple.java b/src/main/java/hu/tilos/radio/backend/recommendation/RecommendationSimple.java index 24bc9fa..6e979fa 100644 --- a/src/main/java/hu/tilos/radio/backend/recommendation/RecommendationSimple.java +++ b/src/main/java/hu/tilos/radio/backend/recommendation/RecommendationSimple.java @@ -1,6 +1,7 @@ package hu.tilos.radio.backend.recommendation; -import hu.tilos.radio.backend.author.AuthorWithContribution; +import hu.tilos.radio.backend.data.Author; + import java.util.Date; public class RecommendationSimple { @@ -11,7 +12,7 @@ public class RecommendationSimple { private String link; private String image; private Date date; - private AuthorWithContribution author; + private Author author; private Date created; public String getId() { @@ -66,11 +67,11 @@ public void setDate(Date date) { this.date = date; } - public AuthorWithContribution getAuthor() { + public Author getAuthor() { return author; } - public void setAuthor(AuthorWithContribution author) { + public void setAuthor(Author author) { this.author = author; } diff --git a/src/main/resources/dozer.xml b/src/main/resources/dozer.xml index 5a37816..ed4ce65 100644 --- a/src/main/resources/dozer.xml +++ b/src/main/resources/dozer.xml @@ -69,10 +69,6 @@ this type - - this - author -