Skip to content

Commit

Permalink
RecommendationService update
Browse files Browse the repository at this point in the history
  • Loading branch information
adalinmik committed May 26, 2024
1 parent 840ba81 commit c97f02f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -27,6 +30,8 @@ public class RecommendationService {
private DB db;

private static final String COLLECTION = "recommendation";
@Autowired
private AuthorRepository authorRepository;

public List<RecommendationSimple> list(String type) {
BasicDBObject criteria = new BasicDBObject();
Expand All @@ -36,7 +41,13 @@ public List<RecommendationSimple> 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<RecommendationSimple> mappedRecommendations = new ArrayList<>();

Expand All @@ -47,14 +58,14 @@ public List<RecommendationSimple> list(String type) {
mappedRecommendations.add(mapper.map(selectedRecommendation, RecommendationSimple.class));
}

/*Collections.sort(mappedRecommendations, new Comparator<RecommendationSimple>() {
@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) {
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 0 additions & 4 deletions src/main/resources/dozer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,6 @@
<a key="type">this</a>
<b>type</b>
</field>
<field>
<a key="author">this</a>
<b>author</b>
</field>
</mapping>

<mapping map-null="false">
Expand Down

0 comments on commit c97f02f

Please sign in to comment.