Skip to content

Commit

Permalink
Merge pull request #29 from dinhphu28/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
dinhphu28 authored Dec 24, 2022
2 parents eebbe70 + 797f59c commit e30f1f3
Show file tree
Hide file tree
Showing 26 changed files with 1,331 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.ndp.knowsharing.Entities.Comment;
import com.ndp.knowsharing.Entities.UserVoteState;
import com.ndp.knowsharing.Models.Article.ArticleCreateModel;
import com.ndp.knowsharing.Models.Article.ArticleHideShowModel;
import com.ndp.knowsharing.Models.Article.ArticleItemReturnModel;
import com.ndp.knowsharing.Models.Article.ArticleUpdateModel;
import com.ndp.knowsharing.Models.Article.PageOfArticleModel;
Expand Down Expand Up @@ -333,6 +334,42 @@ public ResponseEntity<Object> retrieveByUrl(@PathVariable("url") String url) {
return entity;
}

@GetMapping(
value = "/search",
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<Object> searchArticles(@RequestParam(value = "q", required = true) String q, @RequestParam(value = "page", required = true) Integer pageNum) {
ResponseEntity<Object> entity;

PageOfArticleModel pageOfArticleModel = new PageOfArticleModel();

List<ArticleItemReturnModel> articleItemReturnModels = new ArrayList<ArticleItemReturnModel>();

List<Article> articles = articleService.retrieveWithFullTextSearchByTitleAndDescriptionAndContent(q, pageNum);

for (Article article : articles) {
List<UserVoteState> userVoteStates = userVoteStateService.retrieveByArticleId(article.getId());

Integer voteScore = 0;

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

ArticleItemReturnModel articleItemReturnModel = new ArticleItemReturnModel(article, voteScore);

articleItemReturnModels.add(articleItemReturnModel);
}

pageOfArticleModel.setNumberOfPages(articleService.retrieveNumOfPagesWithFullTextSearchByTitleAndDescriptionAndContent(q).intValue());
pageOfArticleModel.setCurrentPage(0);
pageOfArticleModel.setArticles(articleItemReturnModels);

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

return entity;
}

@PostMapping(
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE
Expand Down Expand Up @@ -439,6 +476,37 @@ public ResponseEntity<Object> updateOneArticle(@PathVariable("id") String id, @R
return entity;
}

@PutMapping(
value = "/{articleId}/hide",
produces = MediaType.APPLICATION_JSON_VALUE,
consumes = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<Object> hideShowArticle(@PathVariable("articleId") String articleId, @RequestBody ArticleHideShowModel articleHideShowModel) {
ResponseEntity<Object> entity;

if(articleHideShowModel.getHidden() == null) {
entity = new ResponseEntity<>("{ \"Notice\": \"Not allow null\" }", HttpStatus.BAD_REQUEST);
} else {
Article tmpArticle = articleService.retrieveOne(articleId);

if(tmpArticle == null) {
entity = new ResponseEntity<>("{ \"Notice\": \"Not found\" }", HttpStatus.NOT_FOUND);
} else {
tmpArticle.setHidden(articleHideShowModel.getHidden() ? 1 : 0);

Article tmpSaved = articleService.updateOne(tmpArticle);

if(tmpSaved == null) {
entity = new ResponseEntity<>("{ \"Notice\": \"Failed\" }", HttpStatus.INTERNAL_SERVER_ERROR);
} else {
entity = new ResponseEntity<>(tmpSaved, HttpStatus.OK);
}
}
}

return entity;
}

// For comments

@GetMapping(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ResponseEntity<Object> retrieveAll(@RequestParam(value = "solved", requir

Article article = articleService.retrieveOne(articleReport.getArticleId());

ArticleReportItemModel tmpARIModel = new ArticleReportItemModel(articleReport.getId(), articleReport.getDateCreated(), articleReport.getArticleId(), article.getAuthor(), articleReport.getContent(), article.getTitle(), article.getUrl(), articleReport.getIsSolved());
ArticleReportItemModel tmpARIModel = new ArticleReportItemModel(articleReport.getId(), articleReport.getDateCreated(), articleReport.getArticleId(), articleReport.getAuthor(), articleReport.getContent(), article.getTitle(), article.getUrl(), articleReport.getIsSolved());

articleReportItemModels.add(tmpARIModel);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.ndp.knowsharing.Controllers;

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.GetMapping;
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.Category;
import com.ndp.knowsharing.Models.ArticleForHome.NewlyArticleForHomeReturnModel;
import com.ndp.knowsharing.Services.ArticleService;
import com.ndp.knowsharing.Services.CategoryService;

@RestController
@CrossOrigin("*")
@RequestMapping("/api/v1/new-articles")
public class HomeNewArticleController {

@Autowired
private ArticleService articleService;

@Autowired
private CategoryService categoryService;

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

List<NewlyArticleForHomeReturnModel> newlyArticleForHomeReturnModels = new ArrayList<NewlyArticleForHomeReturnModel>();

List<Article> newestArticles = articleService.retrieveTop5ByHiddenAndNewest(0);

for (Article article : newestArticles) {
Category category = categoryService.retrieveById(article.getCategory());

NewlyArticleForHomeReturnModel tmp = new NewlyArticleForHomeReturnModel(article, category.getName());

newlyArticleForHomeReturnModels.add(tmp);
}

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

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

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

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class ArticleHideShowModel {
private Boolean hidden;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package com.ndp.knowsharing.Models.ArticleForHome;

import java.time.LocalDateTime;

import com.ndp.knowsharing.Entities.Article;

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

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class NewlyArticleForHomeReturnModel {
private String id;

private LocalDateTime dateCreated;

private LocalDateTime dateModified;

private String createdBy;

private String createdByName;

private String modifiedBy;

private String modifiedByName;

private String title;

private String description;

private String content;

private String audioContent;

private String author;

private String url;

private String category;

private String thumbnailUrl;

private Integer hidden;

private String categoryName;

public NewlyArticleForHomeReturnModel(Article article, String categoryName) {
this.id = article.getId();

this.dateCreated = article.getDateCreated();

this.dateModified = article.getDateModified();

this.createdBy = article.getCreatedBy();

this.createdByName = article.getCreatedByName();

this.modifiedBy = article.getModifiedBy();

this.modifiedByName = article.getModifiedByName();

this.title = article.getTitle();

this.description = article.getDescription();

this.content = article.getContent();

this.audioContent = article.getAudioContent();

this.author = article.getAuthor();

this.url = article.getUrl();

this.category = article.getCategory();

this.thumbnailUrl = article.getThumbnailUrl();

this.hidden = article.getHidden();

this.categoryName = categoryName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
@Repository
public interface ArticleRepo extends JpaRepository<Article, String> {

List<Article> findTop5ByHiddenOrderByDateCreatedDesc(Integer hidden);

List<Article> findByUrl(String url);

List<Article> findByCategory(String category, Pageable pageable);
Expand Down Expand Up @@ -56,4 +58,14 @@ public interface ArticleRepo extends JpaRepository<Article, String> {
nativeQuery = true
)
Page<Article> findByCategoryAndHiddenWithTagIds(@Param("tagids") List<String> tagIds, @Param("category") String category, @Param("hidden") Integer hidden, Pageable pageable);

String query5 = "select a.id, a.dateCreated, a.dateModified, a.createdBy, a.createdByName, a.modifiedBy, a.modifiedByName, a.c_title, a.c_description, a.c_content, a.c_audio_content, a.c_author, a.c_url, a.c_category, a.c_thumbnail_url, a.c_hidden from app_fd_article as a where match(a.c_title, a.c_description, a.c_content) against ( :searchstr )";
String countQuery5 = "select count(a.id) from app_fd_article as a where match(a.c_title, a.c_description, a.c_content) against ( :searchstr )";
@Query(
value = query5,
countQuery = countQuery5,
nativeQuery = true
)
Page<Article> findWithFullTextSearchByTitleAndDescriptionAndContent(@Param("searchstr") String searchStr, Pageable pageable);
// List<Article> findWithFullTextSearchByTitleAndDescriptionAndContent(@Param("searchstr") String searchStr);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ public List<Article> retrieveByUrl(String url) {
return repo.findByUrl(url);
}

public List<Article> retrieveTop5ByHiddenAndNewest(Integer hidden) {
return repo.findTop5ByHiddenOrderByDateCreatedDesc(hidden);
}

public List<Article> retrieveWithFullTextSearchByTitleAndDescriptionAndContent(String searchStr, Integer pageNumber) {
List<Article> articles = new ArrayList<Article>();

try {
Page<Article> page = repo.findWithFullTextSearchByTitleAndDescriptionAndContent(searchStr, PageRequest.of(pageNumber, 10));

articles = page.getContent();
} catch (Exception e) {
// TODO: handle exception
}

return articles;
}

public List<Article> retrieveOneCommonPage(Integer pageNumber) {
Page<Article> page = repo.findAll(PageRequest.of(pageNumber, 10, Sort.by("dateCreated").descending()));

Expand Down Expand Up @@ -180,6 +198,20 @@ public Long retrieveNumOfPagesAndHidden(String categoryId, Integer hidden) {
}
}

public Long retrieveNumOfPagesWithFullTextSearchByTitleAndDescriptionAndContent(String searchStr) {
Long numOfPage = Long.valueOf(0);

try {
Page<Article> page = repo.findWithFullTextSearchByTitleAndDescriptionAndContent(searchStr, PageRequest.of(0, 10));

numOfPage = Long.valueOf(page.getTotalPages());
} catch (Exception e) {
// TODO: handle exception
}

return numOfPage;
}

public Article retrieveOne(String id) {
Article sth = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ spring.datasource.password=123456

logging.level.root=WARN

# spring.jpa.show-sql=true
# spring.jpa.properties.hibernate.format_sql=true

# logging.level.org.hibernate.SQL=DEBUG
# logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE

spring.jpa.properties.hibernate.jdbc.batch_size=30
spring.jpa.properties.hibernate.order_inserts=true
spring.jpa.properties.hibernate.order_updates=true
Expand Down
13 changes: 12 additions & 1 deletion front-end/knowledgesharing/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,25 @@ import ScreenReportCommentList from './screens/Reports/Comment/List';
import AdminPage from './screens/Admin/AdminPage';
import ScreenHomePage from './screens/Home/HomePage';
import { Nav, NavItem, NavLink } from 'reactstrap';
import ScreenArticleSearchResult from './screens/ArticleSearchResult/List';


function App() {

const [reloadToggle, setReloadToggle] = useState(false);

const [searchStrValue, setSearchStrValue] = useState("");

const receiveReloadToggle = () => {
setReloadToggle(!reloadToggle);
};

const receiveSearchStr = (searchStr) => {
// console.log("Received Search String: ", searchStr);

setSearchStrValue(searchStr);
}

// console.log("Location: ", window.location.pathname);

return (
Expand All @@ -48,14 +57,16 @@ function App() {
</header> */}
<Suspense fallback={<div>Loading...</div>}>
<BrowserRouter>
<Header />
<Header onHandleChangeSearchStr={receiveSearchStr} />

<Routes>
<Route path="/home/*" element={<ScreenHomePage />} />
<Route path="/articles/*" element={<ScreenMainPage />} />
<Route path="/" element={<Navigate replace to="/home" />} />
<Route path="/create-article" element={<AddArticle />} />

<Route path="/search" element={<ScreenArticleSearchResult searchString={searchStrValue} />} />

<Route path="/article-reports" element={<ScreenReportArticleList />} />
<Route path="/comment-reports" element={<ScreenReportCommentList />} />

Expand Down
6 changes: 6 additions & 0 deletions front-end/knowledgesharing/src/apis/articleApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ const articleApi = {
const url = `/articles/by-url/${articleUrl}`;

return axiosClient.get(url);
},

getWithSearch: (params) => {
const url = '/articles/search';

return axiosClient.get(url, {params});
}
};

Expand Down
Loading

0 comments on commit e30f1f3

Please sign in to comment.