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..8725f42 --- /dev/null +++ b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/AuthorController.java @@ -0,0 +1,18 @@ +package com.liatrio.dojo.devopsknowledgeshareapi; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import java.util.Arrays; +import java.util.List; + +@RestController +public class AuthorController { + + @GetMapping("/authors") + public ResponseEntity> getAuthors() { + // Mocked list of author names, replace with your actual data model if needed + List authors = Arrays.asList("Author One", "Author Two", "Author Three"); + return ResponseEntity.ok(authors); // Return the list with an OK status + } +} \ 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..fb066ef 100644 --- a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostController.java +++ b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostController.java @@ -1,9 +1,13 @@ 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; import javax.servlet.http.HttpServletResponse; + +import java.util.Calendar; import java.util.Collection; import java.util.stream.Collectors; @@ -31,6 +35,25 @@ public Post post(@RequestBody Post post, HttpServletResponse resp) { return repository.save(post); } + @PutMapping("/posts/{id}") + public ResponseEntity putPost(@PathVariable Long id, @RequestBody Post updatedPost) { + return repository.findById(id) + .map(post -> { + post.setFirstName(updatedPost.getFirstName()); + post.setTitle(updatedPost.getTitle()); + try { + post.setLink(updatedPost.getLink()); + } catch (Exception e) { + return new ResponseEntity(HttpStatus.BAD_REQUEST); + } + post.setImageUrl(updatedPost.getImageUrl()); + post.setDatePosted(Calendar.getInstance().getTime()); + Post savedPost = repository.save(post); + return new ResponseEntity(savedPost, HttpStatus.OK); + }) + .orElseGet(() -> 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..2372bfe 100644 --- a/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostRepository.java +++ b/src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/PostRepository.java @@ -3,8 +3,12 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; import org.springframework.stereotype.Repository; +import java.util.List; @RepositoryRestResource @Repository interface PostRepository extends JpaRepository { + List findByTitleContainingIgnoreCase(String title); + List findByFirstNameContainingIgnoreCase(String firstName); + List findByLinkContainingIgnoreCase(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..a6a7f2d --- /dev/null +++ b/src/test/java/com/liatrio/dojo/devopsknowledgeshareapi/AuthorControllerTest.java @@ -0,0 +1,38 @@ +package com.liatrio.dojo.devopsknowledgeshareapi; + +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; +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.test.web.servlet.MockMvc; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; + +@ExtendWith(MockitoExtension.class) +@SpringBootTest +@AutoConfigureMockMvc() +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class AuthorControllerTest { + @Autowired + private MockMvc mockMvc; + + @InjectMocks + AuthorController mockAuthorController; + + @BeforeAll + public void setup() throws Exception { + this.mockMvc = standaloneSetup(mockAuthorController).build(); + } + + @Test + public void getAuthorsResponse() throws Exception { + this.mockMvc.perform(get("/authors")).andDo(print()).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..5c7b39c 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,15 @@ 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); + } +} \ No newline at end of file