Skip to content

Commit

Permalink
Merge branch 'master' of github.com:tilosradio/backend
Browse files Browse the repository at this point in the history
  • Loading branch information
drifteaur committed Mar 14, 2024
2 parents bbaf674 + 8a06521 commit 0c7883b
Show file tree
Hide file tree
Showing 10 changed files with 494 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ env:
MAVEN_OPTS: -Dhttp.keepAlive=false -Dmaven.wagon.http.pool=false -Dmaven.wagon.http.retryHandler.class=standard -Dmaven.wagon.http.retryHandler.count=3
jobs:
compile:
runs-on: ubuntu-18.04
runs-on: ubuntu-20.04
timeout-minutes: 30
steps:
- name: Checkout project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,9 @@ public CreateResponse create(EpisodeToSave entity) {


if (entity.getText() != null) {
entity.getText().setFormat("markdown");
if (entity.getText().getFormat() == null){
entity.getText().setFormat("markdown");
}
entity.getText().setType("episode");
}
if (entity.getRealFrom() == null) {
Expand Down Expand Up @@ -382,7 +384,9 @@ public void delete(String internalId) {
public UpdateResponse update(String alias, EpisodeToSave objectToSave) {

if (objectToSave.getText() != null) {
objectToSave.getText().setFormat("markdown");
if (objectToSave.getText().getFormat() == null){
objectToSave.getText().setFormat("markdown");
}
objectToSave.getText().setType("episode");
}

Expand Down
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);
}
}
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;
}
}
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;
}
}
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;
}
}
Loading

0 comments on commit 0c7883b

Please sign in to comment.