Skip to content

Commit

Permalink
note-> article rename step 1
Browse files Browse the repository at this point in the history
  • Loading branch information
gtiwari333 committed Jan 31, 2020
1 parent 2474ed7 commit 18800b8
Show file tree
Hide file tree
Showing 22 changed files with 80 additions and 80 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# A Spring Boot Web Application Seed with tons of Ready to use features

### Intro
This is a simple micro blogging application where you can post a note/blog and other can view it.
This is a simple micro blogging application where you can post a blog article and other can view it.

The default username/passwords are listed on : gt.app.Application.initData, which are:

Expand Down Expand Up @@ -70,7 +70,7 @@ Future: do more stuff
- Liquibase/Flyway change log
- Integrate Markdown editor for writing notes
- search service using elastic search -- search into uploaded files as well
- rate limit by IP on public API ( note api )
- rate limit by IP on public API ( article api )

## Screenshots:

Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<artifactId>core</artifactId>

<parent>
<artifactId>note-app</artifactId>
<artifactId>article-app</artifactId>
<groupId>gt.app</groupId>
<version>1.0</version>
</parent>
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/gt/app/DataCreator.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package gt.app;

import gt.app.config.Constants;
import gt.app.domain.Article;
import gt.app.domain.Authority;
import gt.app.domain.Note;
import gt.app.domain.NoteStatus;
import gt.app.domain.User;
import gt.app.modules.note.NoteService;
Expand Down Expand Up @@ -93,7 +93,7 @@ public void ctxRefreshed(ContextRefreshedEvent evt) {
}

void createNote(User user, String title, String content) {
var n = new Note();
var n = new Article();
n.setCreatedByUser(user);
n.setTitle(title);
n.setContent(content);
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/gt/app/modules/note/NoteMapper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gt.app.modules.note;

import gt.app.domain.Note;
import gt.app.domain.Article;
import gt.app.domain.ReceivedFile;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
Expand All @@ -15,13 +15,13 @@ public interface NoteMapper {
@Mapping(source = "createdByUser.id", target = "userId")
@Mapping(source = "createdByUser.uniqueId", target = "username")
@Mapping(source = "attachedFiles", target = "files")
NoteReadDto mapForRead(Note note);
NoteReadDto mapForRead(Article article);

@Mapping(target = "id", ignore = true)
@Mapping(target = "attachedFiles", ignore = true)
void createToEntity(NoteEditDto dto, @MappingTarget Note note);
void createToEntity(NoteEditDto dto, @MappingTarget Article article);

Note createToEntity(NoteCreateDto dto);
Article createToEntity(NoteCreateDto dto);

@Mapping(source = "originalFileName", target = "name")
NoteReadDto.FileInfo map(ReceivedFile receivedFile);
Expand Down
14 changes: 7 additions & 7 deletions core/src/main/java/gt/app/modules/note/NoteRepository.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gt.app.modules.note;

import gt.app.domain.Note;
import gt.app.domain.Article;
import gt.app.domain.NoteStatus;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -11,20 +11,20 @@

import java.util.Optional;

interface NoteRepository extends JpaRepository<Note, Long> {
interface NoteRepository extends JpaRepository<Article, Long> {

@EntityGraph(attributePaths = {"attachedFiles", "createdByUser"})
Optional<Note> findWithFilesAndUserById(Long id);
Optional<Article> findWithFilesAndUserById(Long id);

@EntityGraph(attributePaths = {"createdByUser", "attachedFiles"})
Page<Note> findWithFilesAndUserAllByStatus(Pageable pageable, NoteStatus status);
Page<Article> findWithFilesAndUserAllByStatus(Pageable pageable, NoteStatus status);

@EntityGraph(attributePaths = {"createdByUser", "attachedFiles"})
Page<Note> findWithFilesAndUserByCreatedByUser_IdAndStatusOrderByCreatedDateDesc(Pageable pageable, Long userId, NoteStatus status);
Page<Article> findWithFilesAndUserByCreatedByUser_IdAndStatusOrderByCreatedDateDesc(Pageable pageable, Long userId, NoteStatus status);

@Query("select n.createdByUser.id from Note n where n.id=:id ")
@Query("select n.createdByUser.id from Article n where n.id=:id ")
Long findCreatedByUserIdById(@Param("id") Long id);

@EntityGraph(attributePaths = {"attachedFiles", "createdByUser"})
Optional<Note> findWithFilesAndUserByIdAndStatus(Long id, NoteStatus flagged);
Optional<Article> findWithFilesAndUserByIdAndStatus(Long id, NoteStatus flagged);
}
20 changes: 10 additions & 10 deletions core/src/main/java/gt/app/modules/note/NoteService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gt.app.modules.note;

import gt.app.domain.Note;
import gt.app.domain.Article;
import gt.app.domain.NoteStatus;
import gt.app.domain.ReceivedFile;
import gt.app.modules.file.FileService;
Expand All @@ -25,11 +25,11 @@ public class NoteService {
private final NoteRepository noteRepository;
private final FileService fileService;

public Note save(Note note) {
return noteRepository.save(note);
public Article save(Article article) {
return noteRepository.save(article);
}

public Note createNote(NoteCreateDto dto) {
public Article createNote(NoteCreateDto dto) {

List<ReceivedFile> files = new ArrayList<>();
for (MultipartFile mpf : dto.getFiles()) {
Expand All @@ -42,15 +42,15 @@ public Note createNote(NoteCreateDto dto) {
files.add(new ReceivedFile(FILE_GROUP, mpf.getOriginalFilename(), fileId));
}

Note note = NoteMapper.INSTANCE.createToEntity(dto);
note.getAttachedFiles().addAll(files);
Article article = NoteMapper.INSTANCE.createToEntity(dto);
article.getAttachedFiles().addAll(files);

return save(note);
return save(article);
}

public Note update(NoteEditDto dto) {
public Article update(NoteEditDto dto) {

Optional<Note> noteOpt = noteRepository.findWithFilesAndUserById(dto.getId());
Optional<Article> noteOpt = noteRepository.findWithFilesAndUserById(dto.getId());
return noteOpt.map(note -> {
NoteMapper.INSTANCE.createToEntity(dto, note);
return save(note);
Expand Down Expand Up @@ -94,7 +94,7 @@ public Long findCreatedByUserIdById(Long id) {
}


public Optional<Note> handleReview(NoteReviewDto dto) {
public Optional<Article> handleReview(NoteReviewDto dto) {
return noteRepository.findWithFilesAndUserByIdAndStatus(dto.getId(), NoteStatus.FLAGGED)
.map(n -> {
n.setStatus(dto.getVerdict());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gt.app.modules.user;

import gt.app.config.security.AppUserDetails;
import gt.app.domain.Note;
import gt.app.domain.Article;
import gt.app.domain.User;
import gt.app.modules.note.NoteService;
import lombok.RequiredArgsConstructor;
Expand All @@ -28,7 +28,7 @@ public boolean hasAccess(AppUserDetails curUser, Long id, String entity) {
}


if (Note.class.getSimpleName().equalsIgnoreCase(entity)) {
if (Article.class.getSimpleName().equalsIgnoreCase(entity)) {

Long createdById = noteService.findCreatedByUserIdById(id);

Expand Down
6 changes: 3 additions & 3 deletions core/src/main/java/gt/app/web/mvc/IndexController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package gt.app.web.mvc;

import gt.app.config.security.AppUserDetails;
import gt.app.domain.Note;
import gt.app.domain.Article;
import gt.app.modules.note.NoteService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -25,7 +25,7 @@ public String index(Model model, Pageable pageable) {
model.addAttribute("greeting", "Hello Spring");

model.addAttribute("notes", noteService.readAll(PageRequest.of(0, 20, Sort.by("createdDate").descending())));
model.addAttribute("note", new Note());
model.addAttribute("note", new Article());

return "landing";
}
Expand All @@ -40,7 +40,7 @@ public String adminHome(Model model) {
public String userHome(Model model, @AuthenticationPrincipal AppUserDetails principal) {
model.addAttribute("message", getWelcomeMessage(principal));
model.addAttribute("notes", noteService.readAllByUser(PageRequest.of(0, 20, Sort.by("createdDate").descending()), principal.getId()));
model.addAttribute("note", new Note());
model.addAttribute("note", new Article());
return "note";
}

Expand Down
8 changes: 4 additions & 4 deletions core/src/main/java/gt/app/web/mvc/NoteController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gt.app.web.mvc;

import gt.app.domain.Note;
import gt.app.domain.Article;
import gt.app.modules.note.NoteCreateDto;
import gt.app.modules.note.NoteEditDto;
import gt.app.modules.note.NoteService;
Expand All @@ -26,7 +26,7 @@ public class NoteController {
@GetMapping("/add")
public String startAddNote(Model model) {
model.addAttribute("msg", "Add a new note");
model.addAttribute("note", new Note());
model.addAttribute("note", new Article());
return "note/add-note";
}

Expand All @@ -35,9 +35,9 @@ public String finishAddNote(NoteCreateDto noteDto, RedirectAttributes redirectAt

//TODO:validate and return to GET:/add on errors

Note note = noteService.createNote(noteDto);
Article article = noteService.createNote(noteDto);

redirectAttrs.addFlashAttribute("success", "Note with id " + note.getId() + " is created");
redirectAttrs.addFlashAttribute("success", "Note with id " + article.getId() + " is created");

return "redirect:/";
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/java/gt/app/web/mvc/ReviewController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package gt.app.web.mvc;

import gt.app.domain.Note;
import gt.app.domain.Article;
import gt.app.domain.NoteStatus;
import gt.app.modules.note.NoteReviewDto;
import gt.app.modules.note.NoteService;
Expand Down Expand Up @@ -33,7 +33,7 @@ public String startEditNote(Model model, @PathVariable Long id) {
@PostMapping("/review")
public String finishEditNote(NoteReviewDto reviewResult, RedirectAttributes redirectAttrs) {

Optional<Note> noteOpt = noteService.handleReview(reviewResult);
Optional<Article> noteOpt = noteService.handleReview(reviewResult);

String action = reviewResult.getVerdict() == NoteStatus.PUBLISHED ? "Approved" : "Rejected";

Expand Down
2 changes: 1 addition & 1 deletion core/src/main/resources/templates/_fragments/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ <h5 class="my-0 mr-md-auto font-weight-normal">
<nav class="my-2 my-md-0 mr-md-3">

<span sec:authorize="hasRole('USER')">
<a class="p-2" th:href="@{/note}" id="user-note-link">
<a class="p-2" th:href="@{/article}" id="user-article-link">
<span sec:authentication="name">Notes</span>'s Notes
</a>
</span>
Expand Down
14 changes: 7 additions & 7 deletions core/src/main/resources/templates/admin/admin-area.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ <h2><span th:text="${notesToReview.getTotalElements()}">##</span> Notes to revie
</tr>
</thead>
<tbody>
<tr th:each="note : ${notesToReview}">
<td th:text="${note.title}"></td>
<td th:text="${note.content}"></td>
<tr th:each="article : ${notesToReview}">
<td th:text="${article.title}"></td>
<td th:text="${article.content}"></td>
<td>
<span th:each="file : ${note.files}">
<span th:each="file : ${article.files}">
<a th:href="@{/download/file/{id}(id=${file.id})}"> <span
th:text="${file.name}"></span> </a>
</span>
</td>
<td th:text="${#temporals.format(note.createdDate, 'yyyy-MM-dd HH:mm')}"></td>
<td th:text="${note.username}"></td>
<td th:text="${#temporals.format(article.createdDate, 'yyyy-MM-dd HH:mm')}"></td>
<td th:text="${article.username}"></td>
<td>
<a th:href="@{/admin/review/{id}(id=${note.id})}">Review</a>
<a th:href="@{/admin/review/{id}(id=${article.id})}">Review</a>
</td>
</tr>
</tbody>
Expand Down
4 changes: 2 additions & 2 deletions core/src/main/resources/templates/admin/review-note.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
<div class="card-deck mb-3">
<div class="px-3 py-3 pt-md-8 pb-md-8 mx-auto">

<form action="#" method="post" th:action=@{/admin/review} th:object="${note}" enctype="multipart/form-data">
<form action="#" method="post" th:action=@{/admin/review} th:object="${article}" enctype="multipart/form-data">
<input class="form-control" id="id" placeholder="" required="" th:field="*{id}" type="hidden">

<div th:replace="note/_notes :: note-box"></div>
<div th:replace="article/_notes :: article-box"></div>

<button class="btn btn-primary btn-sm btn-block" type="submit" name="verdict" value="PUBLISHED" id="updateNote-btn">Accept</button>
<button class="btn btn-danger btn-sm btn-block" type="submit" name="verdict" value="BLOCKED" id="updateNote-btn">Reject</button>
Expand Down
8 changes: 4 additions & 4 deletions core/src/main/resources/templates/landing.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@
</div>

<div sec:authorize="isAuthenticated()">
<form action="#" method="post" th:action="@{/note/add}" th:object="${note}"
<form action="#" method="post" th:action="@{/article/add}" th:object="${article}"
enctype="multipart/form-data">
<div th:replace="note/_notes :: note-editor"></div>
<div th:replace="article/_notes :: article-editor"></div>

<button class="btn btn-primary btn-sm btn-block" type="submit" id="postNote-btn">Post Note</button>
</form>
<hr/>
</div>

<th:block th:each="note : ${notes}">
<th:block th:replace="note/_notes :: note-box">
<th:block th:each="article : ${notes}">
<th:block th:replace="article/_notes :: article-box">

</th:block>
</th:block>
Expand Down
24 changes: 12 additions & 12 deletions core/src/main/resources/templates/note.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ <h3>
</div>
</div>

<form action="#" method="post" th:action="@{/note/add}" th:object="${note}" enctype="multipart/form-data">
<div th:replace="note/_notes :: note-editor"></div>
<form action="#" method="post" th:action="@{/article/add}" th:object="${article}" enctype="multipart/form-data">
<div th:replace="article/_notes :: article-editor"></div>

<button class="btn btn-primary btn-sm btn-block" type="submit" id="postNote-btn">Post Note</button>
</form>
Expand All @@ -47,24 +47,24 @@ <h2>Notes</h2>
</tr>
</thead>
<tbody>
<tr th:each="note : ${notes}">
<td th:text="${note.status}"></td>
<td th:text="${note.title}"></td>
<td th:text="${note.content}"></td>
<tr th:each="article : ${notes}">
<td th:text="${article.status}"></td>
<td th:text="${article.title}"></td>
<td th:text="${article.content}"></td>
<td>
<span th:each="file : ${note.files}">
<span th:each="file : ${article.files}">
<a th:href="@{/download/file/{id}(id=${file.id})}"> <span th:text="${file.name}"></span> </a>
</span>
</td>
<td th:text="${#temporals.format(note.createdDate, 'yyyy-MM-dd HH:mm')}"></td>
<td th:text="${#temporals.format(article.createdDate, 'yyyy-MM-dd HH:mm')}"></td>
<td>
<span th:if="${#authentication.getPrincipal().getId() == note.userId}">
<a th:href="@{/note/edit/{id}(id=${note.id})}">Edit</a>
<span th:if="${#authentication.getPrincipal().getId() == article.userId}">
<a th:href="@{/article/edit/{id}(id=${article.id})}">Edit</a>
</span>
</td>
<td>
<span th:if="${#authentication.getPrincipal().getId() == note.userId}">
<a th:href="@{/note/delete/{id}(id=${note.id})}">Delete</a>
<span th:if="${#authentication.getPrincipal().getId() == article.userId}">
<a th:href="@{/article/delete/{id}(id=${article.id})}">Delete</a>
</span>
</td>
</tr>
Expand Down
Loading

0 comments on commit 18800b8

Please sign in to comment.