diff --git a/build.gradle b/build.gradle index f2e7794..53e4666 100644 --- a/build.gradle +++ b/build.gradle @@ -14,11 +14,23 @@ repositories { dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' - testImplementation 'org.springframework.boot:spring-boot-starter-test' + implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' + implementation 'org.springframework.boot:spring-boot-devtools' + implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect' + + implementation 'org.webjars:bootstrap:5.1.3' + implementation 'org.webjars:jquery:3.6.0' + implementation 'org.webjars:font-awesome:6.1.0' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'com.h2database:h2:2.1.210' + implementation 'org.hibernate.validator:hibernate-validator' + implementation 'org.springdoc:springdoc-openapi-ui:1.6.5' + + testImplementation 'org.springframework.boot:spring-boot-starter-test' + } tasks.named('test') { diff --git a/data.mv.db b/data.mv.db index 2f670b1..e689d54 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/front/lab4_vue_front b/front/lab4_vue_front index 62649c7..2bc9e21 160000 --- a/front/lab4_vue_front +++ b/front/lab4_vue_front @@ -1 +1 @@ -Subproject commit 62649c78e6740e7f779a68f9588c91a318d4c0b5 +Subproject commit 2bc9e21c22b7fbd6679612d7e6fc1170964cc128 diff --git a/src/main/java/com/webproglabs/lab1/WebConfiguration.java b/src/main/java/com/webproglabs/lab1/WebConfiguration.java index 24be522..0a5a2a5 100644 --- a/src/main/java/com/webproglabs/lab1/WebConfiguration.java +++ b/src/main/java/com/webproglabs/lab1/WebConfiguration.java @@ -2,10 +2,17 @@ package com.webproglabs.lab1; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class WebConfiguration implements WebMvcConfigurer { + public static final String REST_API = "/api"; + @Override + public void addViewControllers(ViewControllerRegistry registry) { + WebMvcConfigurer.super.addViewControllers(registry); + registry.addViewController("rest-test"); + } @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/**").allowedMethods("*"); diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/CommentController.java b/src/main/java/com/webproglabs/lab1/lab34/controller/CommentController.java index 421c70a..c1f7732 100644 --- a/src/main/java/com/webproglabs/lab1/lab34/controller/CommentController.java +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/CommentController.java @@ -1,5 +1,6 @@ package com.webproglabs.lab1.lab34.controller; +import com.webproglabs.lab1.WebConfiguration; import com.webproglabs.lab1.lab34.model.Comment; import com.webproglabs.lab1.lab34.model.Profile; import com.webproglabs.lab1.lab34.services.CommentService; @@ -8,7 +9,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; @RestController -@RequestMapping("/comment") +@RequestMapping(WebConfiguration.REST_API + "/comment") public class CommentController { private final CommentService commentService; diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/CommentDto.java b/src/main/java/com/webproglabs/lab1/lab34/controller/CommentDto.java index 4b1bc5d..6b8cb7d 100644 --- a/src/main/java/com/webproglabs/lab1/lab34/controller/CommentDto.java +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/CommentDto.java @@ -1,5 +1,6 @@ package com.webproglabs.lab1.lab34.controller; +import com.fasterxml.jackson.annotation.JsonProperty; import com.webproglabs.lab1.lab34.model.Comment; public class CommentDto { @@ -11,6 +12,9 @@ public class CommentDto { this.text = comment.getText(); } - public Long getId() {return id;} + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + public long getId() { + return id; + } public String getText() {return text;} } diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/PostController.java b/src/main/java/com/webproglabs/lab1/lab34/controller/PostController.java index da2f2a6..73efcce 100644 --- a/src/main/java/com/webproglabs/lab1/lab34/controller/PostController.java +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/PostController.java @@ -1,5 +1,6 @@ package com.webproglabs.lab1.lab34.controller; +import com.webproglabs.lab1.WebConfiguration; import com.webproglabs.lab1.lab34.services.PostService; import org.springframework.web.bind.annotation.*; @@ -7,7 +8,7 @@ import java.util.ArrayList; import java.util.List; @RestController -@RequestMapping("/post") +@RequestMapping(WebConfiguration.REST_API + "/post") public class PostController { private final PostService postService; diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/PostDto.java b/src/main/java/com/webproglabs/lab1/lab34/controller/PostDto.java index 49075af..0b6f886 100644 --- a/src/main/java/com/webproglabs/lab1/lab34/controller/PostDto.java +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/PostDto.java @@ -1,5 +1,6 @@ package com.webproglabs.lab1.lab34.controller; +import com.fasterxml.jackson.annotation.JsonProperty; import com.webproglabs.lab1.lab34.model.Comment; import com.webproglabs.lab1.lab34.model.Post; import com.webproglabs.lab1.lab34.model.Profile; @@ -12,15 +13,22 @@ public class PostDto { private String text; private List comments = new ArrayList<>(); + private String authorLogin; + public PostDto(Post post){ this.id = post.getId(); this.text = post.getText(); for(Comment comment: post.getComments()){ comments.add(new CommentDto(comment)); } + this.authorLogin = post.getAuthor().getLogin(); } - public Long getId() {return id;} + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + public long getId() { + return id; + } public String getText() {return text;} public List getComments() {return comments;} + public String getAuthor() {return authorLogin;} } diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/ProfileController.java b/src/main/java/com/webproglabs/lab1/lab34/controller/ProfileController.java index 46cb114..a0f80cc 100644 --- a/src/main/java/com/webproglabs/lab1/lab34/controller/ProfileController.java +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/ProfileController.java @@ -1,5 +1,6 @@ package com.webproglabs.lab1.lab34.controller; +import com.webproglabs.lab1.WebConfiguration; import com.webproglabs.lab1.lab34.services.ProfileService; import org.springframework.web.bind.annotation.*; @@ -7,7 +8,7 @@ import java.util.ArrayList; import java.util.List; @RestController -@RequestMapping("/profile") +@RequestMapping(WebConfiguration.REST_API + "/profile") public class ProfileController { private final ProfileService profileService; diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/ProfileDto.java b/src/main/java/com/webproglabs/lab1/lab34/controller/ProfileDto.java index 29f948b..398331f 100644 --- a/src/main/java/com/webproglabs/lab1/lab34/controller/ProfileDto.java +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/ProfileDto.java @@ -1,5 +1,6 @@ package com.webproglabs.lab1.lab34.controller; +import com.fasterxml.jackson.annotation.JsonProperty; import com.webproglabs.lab1.lab34.model.Comment; import com.webproglabs.lab1.lab34.model.Post; import com.webproglabs.lab1.lab34.model.Profile; @@ -26,7 +27,10 @@ public class ProfileDto { } } - public Long getId() {return id;} + @JsonProperty(access = JsonProperty.Access.READ_ONLY) + public long getId() { + return id; + } public String getLogin() {return login;} public String getPassword() {return password;} public List getComments() {return comments;} diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/mvc_controllers/FeedMvcController.java b/src/main/java/com/webproglabs/lab1/lab34/controller/mvc_controllers/FeedMvcController.java new file mode 100644 index 0000000..3acb327 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/mvc_controllers/FeedMvcController.java @@ -0,0 +1,75 @@ +package com.webproglabs.lab1.lab34.controller.mvc_controllers; + +import com.webproglabs.lab1.lab34.controller.PostDto; +import com.webproglabs.lab1.lab34.controller.ProfileDto; +import com.webproglabs.lab1.lab34.model.Post; +import com.webproglabs.lab1.lab34.services.PostService; +import com.webproglabs.lab1.lab34.services.ProfileService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; + +@Controller +@RequestMapping("/feed") +public class FeedMvcController { + + private final ProfileService profileService; + private final PostService postService; + + public FeedMvcController(ProfileService profileService, PostService postService) { + this.profileService = profileService; + this.postService = postService; + } + + @GetMapping + public String getFeedPage(Model model) { + model.addAttribute("profiles", profileService.findAllUsers().stream().map(ProfileDto::new).toList()); + return "feed"; + } + + @GetMapping(value = {"/{id}"}) + public String getFeedPageAuthorized(@PathVariable(required = false) Long id, Model model) { + model.addAttribute("profiles", profileService.findAllUsers().stream().map(ProfileDto::new).toList()); + model.addAttribute("posts", postService.findAllPosts().stream().map(PostDto::new).toList()); + model.addAttribute("selectedProfile", new ProfileDto(profileService.findUser(id))); + + return "feedPosts"; + } + + @GetMapping(value= {"/filter/{id}/"}) + public String getFeedPageFiltered(@PathVariable(required = false) Long id, @RequestParam(value="searchField") String searchField, Model model) { + model.addAttribute("profiles", profileService.findAllUsers().stream().map(ProfileDto::new).toList()); + model.addAttribute("posts", postService.findFilteredPosts(searchField).stream().map(PostDto::new).toList()); + model.addAttribute("selectedProfile", new ProfileDto(profileService.findUser(id))); + return "feedPosts"; + } + + @PostMapping(value={"/post/{id}/"}) + public String createPost(@PathVariable(required = false) Long id, @RequestParam(value="postInputField") String postInputField) { + postService.addPost(postInputField, new ArrayList<>(), id); + return "redirect:/feed/" + id.toString(); + } + + @PostMapping(value = {"/deletePost/{id}/{authorId}"}) + public String deletePost(@PathVariable(required = false) Long id, @PathVariable(required = false) Long authorId) { + postService.deletePost(id); + return "redirect:/feed/" + authorId.toString(); + } + + @GetMapping(value = {"postModal/{id}/{authorId}"}) + public String getPostEditModal(@PathVariable(required = false) Long id,@PathVariable(required = false) Long authorId, Model model) { + model.addAttribute("selectedPost", new PostDto(postService.findPost(id))); + model.addAttribute("profiles", profileService.findAllUsers().stream().map(ProfileDto::new).toList()); + model.addAttribute("posts", postService.findAllPosts().stream().map(PostDto::new).toList()); + model.addAttribute("selectedProfile", new ProfileDto(profileService.findUser(authorId))); + return "editPostModal"; + } + + @PostMapping(value = {"editPost/{id}/{authorId}/"}) + public String editPost(@PathVariable(required = false) Long id, @PathVariable(required = false) Long authorId, @RequestParam(value="postEditField") String postEditField) { + postService.updatePost(id, postEditField); + return "redirect:/feed/" + authorId.toString(); + } +} diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/mvc_controllers/ProfileMvcController.java b/src/main/java/com/webproglabs/lab1/lab34/controller/mvc_controllers/ProfileMvcController.java new file mode 100644 index 0000000..032ba34 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/mvc_controllers/ProfileMvcController.java @@ -0,0 +1,15 @@ +package com.webproglabs.lab1.lab34.controller.mvc_controllers; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping("/") +public class ProfileMvcController { + + @GetMapping + public String index() { + return "default"; + } +} diff --git a/src/main/java/com/webproglabs/lab1/lab34/services/PostService.java b/src/main/java/com/webproglabs/lab1/lab34/services/PostService.java index 43a7584..5b4abf7 100644 --- a/src/main/java/com/webproglabs/lab1/lab34/services/PostService.java +++ b/src/main/java/com/webproglabs/lab1/lab34/services/PostService.java @@ -53,7 +53,7 @@ public class PostService { } @Transactional - public Post addPost(String text, List comments, Long authorId) { + public Post addPost (String text, List comments, Long authorId) { if (!StringUtils.hasText(text)) { throw new IllegalArgumentException("Post data is null or empty"); } diff --git a/src/main/resources/public/css/style.css b/src/main/resources/public/css/style.css new file mode 100644 index 0000000..a68a220 --- /dev/null +++ b/src/main/resources/public/css/style.css @@ -0,0 +1,15 @@ +.container-padding { + padding: 10px; +} + +.margin-bottom { + margin-bottom: 10px; +} + +.button-fixed { + min-width: 120px; +} + +.button-sm { + padding: 1px; +} \ No newline at end of file diff --git a/src/main/resources/public/favicon.svg b/src/main/resources/public/favicon.svg new file mode 100644 index 0000000..c2e8ab2 --- /dev/null +++ b/src/main/resources/public/favicon.svg @@ -0,0 +1,4 @@ + + + \ No newline at end of file diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html new file mode 100644 index 0000000..098c64a --- /dev/null +++ b/src/main/resources/templates/default.html @@ -0,0 +1,38 @@ + + + + + Лабораторная работа 5 + + + + + + + + + + + +
+

Лабораторная работа 5

+
+ + +
+
+
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/editPostModal.html b/src/main/resources/templates/editPostModal.html new file mode 100644 index 0000000..fcb157d --- /dev/null +++ b/src/main/resources/templates/editPostModal.html @@ -0,0 +1,37 @@ + + + + + + +
+ + + +
+ + + + + \ No newline at end of file diff --git a/src/main/resources/templates/feed.html b/src/main/resources/templates/feed.html new file mode 100644 index 0000000..2b02dd5 --- /dev/null +++ b/src/main/resources/templates/feed.html @@ -0,0 +1,36 @@ + + + + + +
+
+ + +
+ + +
+
+ + + + \ No newline at end of file diff --git a/src/main/resources/templates/feedPosts.html b/src/main/resources/templates/feedPosts.html new file mode 100644 index 0000000..e6064e4 --- /dev/null +++ b/src/main/resources/templates/feedPosts.html @@ -0,0 +1,81 @@ + + + + + +
+ +
+

+
+
+ + Лента + +
+ +
+ + +
+ + +
+
+

+
+
+ Автор: + +
+
+
+ +
+
+ +
+
+
+
+
+ + + +
+ + +
+ + + + \ No newline at end of file