-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:tilosradio/backend
- Loading branch information
Showing
10 changed files
with
494 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/hu/tilos/radio/backend/recommendation/RecommendationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package hu.tilos.radio.backend.recommendation; | ||
|
||
import hu.tilos.radio.backend.auth.UserService; | ||
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 org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.validation.annotation.Validated; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.inject.Inject; | ||
import java.util.List; | ||
|
||
@RestController | ||
public class RecommendationController { | ||
|
||
@Inject | ||
private RecommendationService recommendationService; | ||
|
||
@Inject | ||
private UserService userService; | ||
|
||
@RequestMapping(value = "/api/v1/recommendation", method = RequestMethod.GET) | ||
public List<RecommendationSimple> list(@RequestParam(name = "type", defaultValue = "all", required = false) String type) { | ||
return recommendationService.list(type); | ||
} | ||
|
||
@RequestMapping(value = "/api/v1/recommendation/{id}", method = RequestMethod.GET) | ||
public RecommendationData get(@PathVariable String id) { | ||
return recommendationService.get(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()); | ||
} | ||
|
||
@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); | ||
} | ||
|
||
@PreAuthorize("hasRole('ROLE_AUTHOR')") | ||
@RequestMapping(value = "/api/v1/recommendation/{id}", method = RequestMethod.DELETE) | ||
public OkResponse delete(@PathVariable String id) { | ||
return recommendationService.delete(id); | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
src/main/java/hu/tilos/radio/backend/recommendation/RecommendationData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package hu.tilos.radio.backend.recommendation; | ||
|
||
import hu.tilos.radio.backend.data.Author; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.data.mongodb.core.mapping.Document; | ||
|
||
import javax.validation.constraints.NotNull; | ||
import java.util.Date; | ||
|
||
@Document(collection = "recommendation") | ||
public class RecommendationData { | ||
|
||
@Id | ||
private String id; | ||
|
||
@NotNull | ||
private RecommendationType type; | ||
|
||
@NotNull | ||
private String title; | ||
|
||
private String description; | ||
|
||
private String writer; | ||
|
||
private String link; | ||
|
||
private String image; | ||
|
||
private Date date; | ||
|
||
private Date created; | ||
|
||
private Author author; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public RecommendationType getType() { | ||
return type; | ||
} | ||
|
||
public void setType(RecommendationType type) { | ||
this.type = type; | ||
} | ||
|
||
public void setType(String type) { | ||
this.type = RecommendationType.valueOf(type); | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
|
||
public Date getDate() { | ||
return date; | ||
} | ||
|
||
public void setDate(Date date) { | ||
this.date = date; | ||
} | ||
|
||
public Date getCreated() { | ||
return created; | ||
} | ||
|
||
public void setCreated(Date createdAt) { | ||
this.created = createdAt; | ||
} | ||
|
||
public Author getAuthor() { | ||
return author; | ||
} | ||
|
||
public void setAuthor(Author author) { | ||
this.author = author; | ||
} | ||
|
||
public String getLink() { | ||
return link; | ||
} | ||
|
||
public void setLink(String link) { | ||
this.link = link; | ||
} | ||
|
||
public String getImage() { | ||
return image; | ||
} | ||
|
||
public void setImage(String image) { | ||
this.image = image; | ||
} | ||
|
||
public String getWriter() { | ||
return writer; | ||
} | ||
|
||
public void setWriter(String writer) { | ||
this.writer = writer; | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
src/main/java/hu/tilos/radio/backend/recommendation/RecommendationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package hu.tilos.radio.backend.recommendation; | ||
|
||
import com.mongodb.*; | ||
import hu.tilos.radio.backend.auth.UserInfo; | ||
import hu.tilos.radio.backend.data.Author; | ||
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 org.bson.types.ObjectId; | ||
import org.dozer.DozerBeanMapper; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.inject.Inject; | ||
import java.util.*; | ||
|
||
@Service | ||
public class RecommendationService { | ||
private static Logger LOG = LoggerFactory.getLogger(RecommendationService.class); | ||
|
||
@Inject | ||
private DozerBeanMapper mapper; | ||
|
||
@Inject | ||
private DB db; | ||
|
||
private static final String COLLECTION = "recommendation"; | ||
|
||
public List<RecommendationSimple> list(String type) { | ||
BasicDBObject criteria = new BasicDBObject(); | ||
|
||
// Filter by type | ||
if (!"all".equals(type) && type != null) { | ||
criteria.put("type", RecommendationType.valueOf(type).ordinal()); | ||
} | ||
|
||
DBCursor selectedRecommendations = db.getCollection(COLLECTION).find(criteria).sort(new BasicDBObject("name", 1)); | ||
|
||
List<RecommendationSimple> mappedRecommendations = new ArrayList<>(); | ||
|
||
for (DBObject selectedRecommendation : selectedRecommendations) { | ||
selectedRecommendation.put("type", RecommendationType.values()[((Integer) selectedRecommendation.get("type"))]); | ||
selectedRecommendation.put("id", selectedRecommendation.get("_id").toString()); | ||
|
||
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()); | ||
} | ||
}); | ||
return mappedRecommendations; | ||
|
||
} | ||
|
||
public RecommendationData get(String id) { | ||
DBObject recommendationObject = db.getCollection(COLLECTION).findOne(new BasicDBObject("_id", new ObjectId(id))); | ||
|
||
RecommendationData recommendation = mapper.map(recommendationObject, RecommendationData.class); | ||
|
||
return recommendation; | ||
} | ||
|
||
public CreateResponse create(RecommendationToSave recommendationToSave, UserInfo userInfo) { | ||
Author author; | ||
|
||
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(); | ||
authorObj.put("alias", author.getAlias()); | ||
authorObj.put("name", author.getName()); | ||
authorObj.put("ref", new DBRef("author", author.getId())); | ||
recommendation.put("author", authorObj); | ||
|
||
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); | ||
db.getCollection(COLLECTION).update(new BasicDBObject("_id", new ObjectId(id)), recommendationObject); | ||
|
||
return new UpdateResponse(true); | ||
} | ||
|
||
public OkResponse delete(String id) { | ||
RecommendationData recommendation = get(id); | ||
if (recommendation == null) { | ||
throw new IllegalArgumentException("Recommendation not found: " + id); | ||
} | ||
|
||
db.getCollection(COLLECTION).remove(new BasicDBObject("_id", new ObjectId(id))); | ||
|
||
return new OkResponse("Recommendation has been deleted"); | ||
} | ||
|
||
public void setDb(DB db) { | ||
this.db = db; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/main/java/hu/tilos/radio/backend/recommendation/RecommendationSimple.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package hu.tilos.radio.backend.recommendation; | ||
|
||
import hu.tilos.radio.backend.author.AuthorWithContribution; | ||
import java.util.Date; | ||
|
||
public class RecommendationSimple { | ||
private String id; | ||
private RecommendationType type; | ||
private String title; | ||
private String writer; | ||
private String link; | ||
private String image; | ||
private Date date; | ||
private AuthorWithContribution author; | ||
|
||
public String getId() { | ||
return id; | ||
} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public RecommendationType getType() { | ||
return type; | ||
} | ||
|
||
public void setType(RecommendationType type) { | ||
this.type = type; | ||
} | ||
|
||
public void setType(String type) { | ||
this.type = RecommendationType.valueOf(type); | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
this.title = title; | ||
} | ||
|
||
public String getLink() { | ||
return link; | ||
} | ||
|
||
public void setLink(String link) { | ||
this.link = link; | ||
} | ||
|
||
public String getImage() { | ||
return image; | ||
} | ||
|
||
public void setImage(String image) { | ||
this.image = image; | ||
} | ||
|
||
public Date getDate() { | ||
return date; | ||
} | ||
|
||
public void setDate(Date date) { | ||
this.date = date; | ||
} | ||
|
||
public AuthorWithContribution getAuthor() { | ||
return author; | ||
} | ||
|
||
public void setAuthor(AuthorWithContribution author) { | ||
this.author = author; | ||
} | ||
|
||
public String getWriter() { | ||
return writer; | ||
} | ||
|
||
public void setWriter(String writer) { | ||
this.writer = writer; | ||
} | ||
} |
Oops, something went wrong.