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 e643e82..a092f05 100644 --- a/src/main/java/hu/tilos/radio/backend/recommendation/RecommendationService.java +++ b/src/main/java/hu/tilos/radio/backend/recommendation/RecommendationService.java @@ -3,6 +3,7 @@ import com.mongodb.*; 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; @@ -35,7 +36,7 @@ public List list(String type) { criteria.put("type", RecommendationType.valueOf(type).ordinal()); } - DBCursor selectedRecommendations = db.getCollection(COLLECTION).find(criteria).sort(new BasicDBObject("name", 1)); + DBCursor selectedRecommendations = db.getCollection(COLLECTION).find(criteria).sort(new BasicDBObject("created", -1)); List mappedRecommendations = new ArrayList<>(); @@ -46,12 +47,12 @@ public List list(String type) { mappedRecommendations.add(mapper.map(selectedRecommendation, RecommendationSimple.class)); } - Collections.sort(mappedRecommendations, new Comparator() { + /*Collections.sort(mappedRecommendations, new Comparator() { @Override public int compare(RecommendationSimple s1, RecommendationSimple s2) { return s1.getTitle().toLowerCase().compareTo(s2.getTitle().toLowerCase()); } - }); + });*/ return mappedRecommendations; } @@ -59,6 +60,10 @@ public int compare(RecommendationSimple s1, RecommendationSimple s2) { public RecommendationData get(String id) { DBObject recommendationObject = db.getCollection(COLLECTION).findOne(new BasicDBObject("_id", new ObjectId(id))); + if (recommendationObject == null) { + throw new NotFoundException("Recommendation not found: " + id); + } + RecommendationData recommendation = mapper.map(recommendationObject, RecommendationData.class); return recommendation; @@ -67,29 +72,43 @@ public RecommendationData get(String id) { public CreateResponse create(RecommendationToSave recommendationToSave, UserInfo userInfo) { Author author; - if (recommendationToSave.getAuthor() != null) { + /*author = recommendationToSave.getAuthor();*/ + + /*if (recommendationToSave.getAuthor() != null) { author = recommendationToSave.getAuthor(); } else 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(); + /*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("author", authorObj);*/ + + recommendation.put("created", new Date()); db.getCollection(COLLECTION).insert(recommendation); return new CreateResponse(((ObjectId) recommendation.get("_id")).toHexString()); } - public UpdateResponse update(String id, RecommendationToSave recommendation) { - DBObject recommendationObject = mapper.map(recommendation, BasicDBObject.class); + public UpdateResponse update(String id, RecommendationToSave recommendationToSave) { + /*Author author = recommendationToSave.getAuthor();*/ + + DBObject recommendationObject = 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()));*/ + /*recommendationObject.put("author", authorObj);*/ + + /*DBObject recommendationObject = mapper.map(recommendationToSave, BasicDBObject.class);*/ db.getCollection(COLLECTION).update(new BasicDBObject("_id", new ObjectId(id)), recommendationObject); return new UpdateResponse(true); 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 9128c6c..24bc9fa 100644 --- a/src/main/java/hu/tilos/radio/backend/recommendation/RecommendationSimple.java +++ b/src/main/java/hu/tilos/radio/backend/recommendation/RecommendationSimple.java @@ -12,6 +12,7 @@ public class RecommendationSimple { private String image; private Date date; private AuthorWithContribution author; + private Date created; public String getId() { return id; @@ -80,4 +81,12 @@ public String getWriter() { public void setWriter(String writer) { this.writer = writer; } + + public Date getCreated() { + return created; + } + + public void setCreated(Date createdAt) { + this.created = createdAt; + } }