Skip to content

Commit

Permalink
feat: Add dateUpdated field to Post model and update PostController t…
Browse files Browse the repository at this point in the history
…o handle PUT requests
  • Loading branch information
baserrato committed Jun 13, 2024
1 parent 0665404 commit 1d1f6a8
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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<List<String>> getAuthors() {
// Mocked list of author names, replace with your actual data model if needed
List<String> authors = Arrays.asList("Author One", "Author Two", "Author Three");
return ResponseEntity.ok(authors); // Return the list with an OK status
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/liatrio/dojo/devopsknowledgeshareapi/Post.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -31,6 +35,25 @@ public Post post(@RequestBody Post post, HttpServletResponse resp) {
return repository.save(post);
}

@PutMapping("/posts/{id}")
public ResponseEntity<Post> 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<Post>(HttpStatus.BAD_REQUEST);
}
post.setImageUrl(updatedPost.getImageUrl());
post.setDatePosted(Calendar.getInstance().getTime());
Post savedPost = repository.save(post);
return new ResponseEntity<Post>(savedPost, HttpStatus.OK);
})
.orElseGet(() -> new ResponseEntity<Post>(HttpStatus.NOT_FOUND));
}

@DeleteMapping("/posts/{id}")
public void deletePost(@PathVariable("id") String id) {
log.info("{}: recieved a DELETE request", deploymentType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Post, Long> {
List<Post> findByTitleContainingIgnoreCase(String title);
List<Post> findByFirstNameContainingIgnoreCase(String firstName);
List<Post> findByLinkContainingIgnoreCase(String link);
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 1d1f6a8

Please sign in to comment.