Skip to content

Commit

Permalink
Merge pull request #27 from dinhphu28/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dinhphu28 authored Dec 21, 2022
2 parents c9ce979 + 5fa6a93 commit eebbe70
Show file tree
Hide file tree
Showing 30 changed files with 1,150 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,34 @@ public ResponseEntity<Object> retrieveById(@PathVariable("id") String id) {
return entity;
}

@GetMapping(
value = "/by-url/{url}",
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<Object> retrieveByUrl(@PathVariable("url") String url) {
ResponseEntity<Object> entity;

List<Article> articles = articleService.retrieveByUrl(url);

if(articles.size() > 0) {
List<UserVoteState> userVoteStates = userVoteStateService.retrieveByArticleId(articles.get(0).getId());

Integer voteScore = 0;

for(UserVoteState uvsItem : userVoteStates) {
voteScore = voteScore + uvsItem.getVoteState();
}

ArticleItemReturnModel articleItemReturnModel = new ArticleItemReturnModel(articles.get(0), voteScore);

entity = new ResponseEntity<>(articleItemReturnModel, HttpStatus.OK);
} else {
entity = new ResponseEntity<>("{ \"Notice\": \"Not found\" }", HttpStatus.NOT_FOUND);
}

return entity;
}

@PostMapping(
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ndp.knowsharing.Entities.ArticleTag;
Expand All @@ -35,12 +36,18 @@ public class ArticleTagController {
@GetMapping(
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<Object> retrieveAll() {
public ResponseEntity<Object> retrieveAll(@RequestParam(value = "category", required = false) String categoryId) {
ResponseEntity<Object> entity;

List<ArticleTagReturnModel> articleTagReturnModels = new ArrayList<ArticleTagReturnModel>();

List<ArticleTag> articleTags = articleTagService.retrieveAll();
List<ArticleTag> articleTags;

if(categoryId == null) {
articleTags = articleTagService.retrieveAll();
} else {
articleTags = articleTagService.retrieveByCategory(categoryId);
}

for (ArticleTag articleTag : articleTags) {
Category category = categoryService.retrieveById(articleTag.getCategory());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.ndp.knowsharing.Controllers;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ndp.knowsharing.Entities.Article;
import com.ndp.knowsharing.Entities.ArticleForHome;
import com.ndp.knowsharing.Models.ArticleForHome.ArticleForHomeReturnModel;
import com.ndp.knowsharing.Models.ArticleForHome.ArticleForHomeUpdateModel;
import com.ndp.knowsharing.Services.ArticleForHomeService;
import com.ndp.knowsharing.Services.ArticleService;

@RestController
@CrossOrigin("*")
@RequestMapping("/api/v1/leader-board-articles")
public class LeaderBoardArticleController {
private final String TYPE = "leader-board";

@Autowired
private ArticleForHomeService articleForHomeService;

@Autowired
private ArticleService articleService;

@GetMapping(
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<Object> retrieveAll() {
ResponseEntity<Object> entity;

List<ArticleForHomeReturnModel> articleForHomeReturnModels = new ArrayList<ArticleForHomeReturnModel>();

List<ArticleForHome> nominatedArticles = articleForHomeService.retrieveByType(TYPE);

for (ArticleForHome articleForHome : nominatedArticles) {
Article tmpArticle = articleService.retrieveOne(articleForHome.getArticleId());

if(tmpArticle != null) {
if(tmpArticle.getHidden() == 0) {
ArticleForHomeReturnModel articleForHomeReturnModel = new ArticleForHomeReturnModel(articleForHome.getId(), articleForHome.getDateCreated(), articleForHome.getDateModified(), articleForHome.getType(), tmpArticle.getId(), tmpArticle.getDateCreated(), tmpArticle.getTitle(), tmpArticle.getContent(), tmpArticle.getDescription(), tmpArticle.getAuthor(), tmpArticle.getUrl(), tmpArticle.getCategory(), tmpArticle.getThumbnailUrl(), tmpArticle.getHidden());

articleForHomeReturnModels.add(articleForHomeReturnModel);
}
}
}

entity = new ResponseEntity<>(articleForHomeReturnModels, HttpStatus.OK);

return entity;
}

@PutMapping(
value = "/{id}",
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<Object> updateById(@PathVariable("id") String id, @RequestBody ArticleForHomeUpdateModel articleForHomeUpdateModel) {
ResponseEntity<Object> entity;

if(articleForHomeUpdateModel.getArticleId().isBlank()) {
entity = new ResponseEntity<>("{ \"Notice\": \"Failed\" }", HttpStatus.BAD_REQUEST);
} else {
LocalDateTime now = LocalDateTime.now();

ArticleForHome articleForHome = new ArticleForHome(id, articleForHomeUpdateModel.getArticleId(), now, now, TYPE);

ArticleForHome tmpSaved = articleForHomeService.saveOne(articleForHome);

entity = new ResponseEntity<>(tmpSaved, HttpStatus.OK);
}

return entity;
}

@DeleteMapping(
value = "/{id}",
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<Object> deleteById(@PathVariable("id") String id) {
ResponseEntity<Object> entity;

Boolean isSuccess = articleForHomeService.deleteById(id);

entity = new ResponseEntity<>("{ \"isSuccess\": " + isSuccess + " }", HttpStatus.OK);

return entity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.ndp.knowsharing.Controllers;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ndp.knowsharing.Entities.Article;
import com.ndp.knowsharing.Entities.ArticleForHome;
import com.ndp.knowsharing.Models.ArticleForHome.ArticleForHomeReturnModel;
import com.ndp.knowsharing.Models.ArticleForHome.ArticleForHomeUpdateModel;
import com.ndp.knowsharing.Services.ArticleForHomeService;
import com.ndp.knowsharing.Services.ArticleService;

@RestController
@CrossOrigin("*")
@RequestMapping("/api/v1/nominated-articles")
public class NominatedArticleController {
private final String TYPE = "nomination";

@Autowired
private ArticleForHomeService articleForHomeService;

@Autowired
private ArticleService articleService;

@GetMapping(
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<Object> retrieveAll() {
ResponseEntity<Object> entity;

List<ArticleForHomeReturnModel> articleForHomeReturnModels = new ArrayList<ArticleForHomeReturnModel>();

List<ArticleForHome> nominatedArticles = articleForHomeService.retrieveByType(TYPE);

for (ArticleForHome articleForHome : nominatedArticles) {
Article tmpArticle = articleService.retrieveOne(articleForHome.getArticleId());

if(tmpArticle != null) {
if(tmpArticle.getHidden() == 0) {
ArticleForHomeReturnModel articleForHomeReturnModel = new ArticleForHomeReturnModel(articleForHome.getId(), articleForHome.getDateCreated(), articleForHome.getDateModified(), articleForHome.getType(), tmpArticle.getId(), tmpArticle.getDateCreated(), tmpArticle.getTitle(), tmpArticle.getContent(), tmpArticle.getDescription(), tmpArticle.getAuthor(), tmpArticle.getUrl(), tmpArticle.getCategory(), tmpArticle.getThumbnailUrl(), tmpArticle.getHidden());

articleForHomeReturnModels.add(articleForHomeReturnModel);
}
}
}

entity = new ResponseEntity<>(articleForHomeReturnModels, HttpStatus.OK);

return entity;
}

@PutMapping(
value = "/{id}",
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<Object> updateById(@PathVariable("id") String id, @RequestBody ArticleForHomeUpdateModel articleForHomeUpdateModel) {
ResponseEntity<Object> entity;

if(articleForHomeUpdateModel.getArticleId().isBlank()) {
entity = new ResponseEntity<>("{ \"Notice\": \"Failed\" }", HttpStatus.BAD_REQUEST);
} else {
LocalDateTime now = LocalDateTime.now();

ArticleForHome articleForHome = new ArticleForHome(id, articleForHomeUpdateModel.getArticleId(), now, now, TYPE);

ArticleForHome tmpSaved = articleForHomeService.saveOne(articleForHome);

entity = new ResponseEntity<>(tmpSaved, HttpStatus.OK);
}

return entity;
}

@DeleteMapping(
value = "/{id}",
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<Object> deleteById(@PathVariable("id") String id) {
ResponseEntity<Object> entity;

Boolean isSuccess = articleForHomeService.deleteById(id);

entity = new ResponseEntity<>("{ \"isSuccess\": " + isSuccess + " }", HttpStatus.OK);

return entity;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.ndp.knowsharing.Entities;

import java.time.LocalDateTime;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(name = "app_fd_articles_for_home")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ArticleForHome {
@Id
// @GeneratedValue(generator = "UUID")
// @GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
@Column(name = "id")
private String id;

@Column(name = "c_article_id")
private String articleId;

@Column(name = "datecreated")
private LocalDateTime dateCreated;

@Column(name = "datemodified")
private LocalDateTime dateModified;

@Column(name = "c_type")
private String type;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.ndp.knowsharing.Models.ArticleForHome;

import java.time.LocalDateTime;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ArticleForHomeReturnModel {

private String id;

private LocalDateTime dateCreated;

private LocalDateTime dateModified;

private String type;

private String articleId;

private LocalDateTime dateCreatedArticle;

private String title;

private String content;

private String description;

private String author;

private String url;

private String category;

private String thumbnailUrl;

private Integer hidden;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ndp.knowsharing.Models.ArticleForHome;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ArticleForHomeUpdateModel {
private String articleId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.ndp.knowsharing.Repositories;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.ndp.knowsharing.Entities.ArticleForHome;

@Repository
public interface ArticleForHomeRepo extends JpaRepository<ArticleForHome, String> {

List<ArticleForHome> findByType(String type);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

@Repository
public interface ArticleRepo extends JpaRepository<Article, String> {

List<Article> findByUrl(String url);

List<Article> findByCategory(String category, Pageable pageable);

List<Article> findByCategoryAndHidden(String category, Integer hidden, Pageable pageable);
Expand Down
Loading

0 comments on commit eebbe70

Please sign in to comment.