From 1142f8d2f2d885a2977ed997c93cb5d398a46d63 Mon Sep 17 00:00:00 2001 From: baserrato Date: Mon, 10 Jun 2024 13:37:56 -0700 Subject: [PATCH] feat: Add dateUpdated field to Post model and update PostController --- .../AuthorController.java | 20 +++++++++++ .../AuthorService.java | 15 ++++++++ .../dojo/devopsknowledgeshareapi/Post.java | 10 ++++++ .../PostController.java | 35 ++++++++++++++++++ .../PostRepository.java | 6 ++++ .../AuthorControllerTest.java | 36 +++++++++++++++++++ .../devopsknowledgeshareapi/PostTest.java | 27 ++++++++++++++ 7 files changed, 149 insertions(+) create mode 100644 src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/AuthorController.java create mode 100644 src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/AuthorService.java create mode 100644 src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/AuthorControllerTest.java diff --git a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/AuthorController.java b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/AuthorController.java new file mode 100644 index 0000000..ed32625 --- /dev/null +++ b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/AuthorController.java @@ -0,0 +1,20 @@ +package com.liatrio.dojo.devopsknowledgeshareapi; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import com.liatrio.dojo.devopsknowledgeshareapi.AuthorService; + + +@RestController +public class AuthorController { + + @Autowired + private AuthorService authorService; + + @GetMapping("/authors") + public Object getAuthors() { + // TODO: Replace Object with your Author class + return authorService.getAuthors(); + } +} \ No newline at end of file diff --git a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/AuthorService.java b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/AuthorService.java new file mode 100644 index 0000000..1714879 --- /dev/null +++ b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/AuthorService.java @@ -0,0 +1,15 @@ +package com.liatrio.dojo.devopsknowledgeshareapi; + +import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; + +@Service +public class AuthorService { + + public List getAuthors() { + // TODO: Replace Object with your Author class + // TODO: Implement the logic to fetch and return the list of authors + return new ArrayList<>(); + } +} \ No newline at end of file diff --git a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/Post.java b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/Post.java index a7429f2..ca92c7e 100644 --- a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/Post.java +++ b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/Post.java @@ -25,6 +25,16 @@ public class Post { private @NonNull String datePosted; private @NonNull String imageUrl; private @NonNull Date dateAsDate; + private String dateUpdated; + + public void setDateUpdated(Date dateAsDate) { + DateFormat dateFormat = new SimpleDateFormat(dateFormat()); + this.dateUpdated = dateFormat.format(dateAsDate); + } + + public String getDateUpdated() { + return dateUpdated; + } public Post() { this.dateAsDate = Calendar.getInstance().getTime(); diff --git a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostController.java b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostController.java index 1fb7569..0ec483d 100644 --- a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostController.java +++ b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostController.java @@ -1,5 +1,7 @@ package com.liatrio.dojo.devopsknowledgeshareapi; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import lombok.extern.slf4j.Slf4j; @@ -25,12 +27,45 @@ public Collection posts() { return repository.findAll().stream().collect(Collectors.toList()); } + @GetMapping("/posts/title/{title}") + public Collection getPostsByTitle(@PathVariable String title) { + return repository.findByTitle(title); + } + + @GetMapping("/posts/firstName/{firstName}") + public Collection getPostsByFirstName(@PathVariable String firstName) { + return repository.findByFirstName(firstName); + } + + @GetMapping("/posts/link/{link}") + public Collection getPostsByLink(@PathVariable String link) { + return repository.findByLink(link); + } + @PostMapping("/posts") public Post post(@RequestBody Post post, HttpServletResponse resp) { log.info("{}: recieved a POST request", deploymentType); return repository.save(post); } + @PutMapping("/posts/{id}") + public ResponseEntity putPost(@PathVariable("id") Long id, @RequestBody Post post) { + log.info("{}: received a PUT request", deploymentType); + return repository.findById(id) + .map(existingPost -> { + existingPost.setTitle(post.getTitle()); + existingPost.setFirstName(post.getFirstName()); + try { + existingPost.setLink(post.getLink()); + } catch (Exception e) { + e.printStackTrace(); + // handle exception + } + return new ResponseEntity<>(repository.save(existingPost), HttpStatus.OK); + }) + .orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND)); + } + @DeleteMapping("/posts/{id}") public void deletePost(@PathVariable("id") String id) { log.info("{}: recieved a DELETE request", deploymentType); diff --git a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostRepository.java b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostRepository.java index 1b28be7..92655f4 100644 --- a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostRepository.java +++ b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostRepository.java @@ -3,8 +3,14 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; import org.springframework.stereotype.Repository; +import org.springframework.data.jpa.repository.JpaRepository; +import com.liatrio.dojo.devopsknowledgeshareapi.Post; +import java.util.List; @RepositoryRestResource @Repository interface PostRepository extends JpaRepository { + List findByTitle(String title); + List findByFirstName(String firstName); + List findByLink(String link); } diff --git a/src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/AuthorControllerTest.java b/src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/AuthorControllerTest.java new file mode 100644 index 0000000..6c8ea90 --- /dev/null +++ b/src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/AuthorControllerTest.java @@ -0,0 +1,36 @@ +package com.liatrio.dojo.devopsknowledgeshareapi; + +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.web.servlet.MockMvc; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import java.util.ArrayList; + +import static org.mockito.Mockito.when; + +import com.liatrio.dojo.devopsknowledgeshareapi.AuthorService; + +@SpringBootTest +@AutoConfigureMockMvc +public class AuthorControllerTest { + + @Autowired + private MockMvc mockMvc; + + @MockBean + private AuthorService authorService; + + @Test + public void getAuthorsTest() throws Exception { + when(authorService.getAuthors()).thenReturn(new ArrayList<>()); + + mockMvc.perform(get("/authors")) + .andExpect(status().isOk()); + } +} \ No newline at end of file diff --git a/src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/PostTest.java b/src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/PostTest.java index df33dd8..55a9caf 100644 --- a/src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/PostTest.java +++ b/src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/PostTest.java @@ -2,6 +2,11 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; + import static org.junit.jupiter.api.Assertions.assertNotEquals; import org.junit.jupiter.api.Test; @@ -106,4 +111,26 @@ public void getImageUrlTest() throws Exception { String test = hc.getImageUrl(); assertEquals(imageUrl, test); } + + @Test + public void setDateUpdatedTest() throws Exception { + Post hc = new Post(); + Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2022-01-01"); + hc.setDateUpdated(date); + String test = hc.getDateUpdated(); + DateFormat dateFormat = new SimpleDateFormat(hc.dateFormat()); + String expected = dateFormat.format(date); + assertEquals(expected, test); + } + + @Test + public void getDateUpdatedTest() throws Exception { + Post hc = new Post(); + Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2022-01-01"); + hc.setDateUpdated(date); + String test = hc.getDateUpdated(); + DateFormat dateFormat = new SimpleDateFormat(hc.dateFormat()); + String expected = dateFormat.format(date); + assertEquals(expected, test); + } }