Skip to content

Commit

Permalink
add code along branch
Browse files Browse the repository at this point in the history
  • Loading branch information
rieckpil committed Aug 10, 2023
1 parent e83ba4a commit 630876d
Show file tree
Hide file tree
Showing 14 changed files with 3 additions and 533 deletions.
38 changes: 1 addition & 37 deletions src/main/java/de/rieckpil/CommentApiController.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,9 @@
package de.rieckpil;

import java.util.List;
import java.util.UUID;

import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

/**
* Develop a REST API to retrieve and create comments. Everybody should be able to retrieve comments
* but only logged-in users with the role ADMIN can create a comment.
*/
@RestController
@RequestMapping("/api/comments")
public class CommentApiController {

private final CommentService commentService;

public CommentApiController(CommentService commentService) {
this.commentService = commentService;
}

@GetMapping
public List<Comment> getAllComments() {
return commentService.findAll();
}

@PostMapping
public ResponseEntity<Void> createComment(
@Valid @RequestBody CommentCreationRequest request,
Authentication authentication,
UriComponentsBuilder uriComponentsBuilder) {

UUID newCommentId = commentService.createComment(request.content(), authentication.getName());

UriComponents uriComponents =
uriComponentsBuilder.path("/api/comments/{id}").buildAndExpand(newCommentId);

return ResponseEntity.created(uriComponents.toUri()).build();
}
}
public class CommentApiController {}
5 changes: 0 additions & 5 deletions src/main/java/de/rieckpil/CommentCreationRequest.java

This file was deleted.

17 changes: 0 additions & 17 deletions src/main/java/de/rieckpil/CommentService.java

This file was deleted.

6 changes: 0 additions & 6 deletions src/main/java/de/rieckpil/Post.java

This file was deleted.

46 changes: 1 addition & 45 deletions src/main/java/de/rieckpil/PostClient.java
Original file line number Diff line number Diff line change
@@ -1,52 +1,8 @@
package de.rieckpil;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.WebClient;

/**
* Develop an HTTP client that fetch all posts from the post-service and return them as a list. The
* post-service returns the all posts with pagination. The client should fetch all pages and return
* the result as a list.
*/
@Component
public class PostClient {

private final WebClient postWebClient;

public PostClient(WebClient postWebClient) {
this.postWebClient = postWebClient;
}

public List<Post> fetchAllPosts() {

int limit = 30;
int skip = 0;
long totalResult = 0;

List<Post> allPosts = new ArrayList<>();

do {
PostResult result =
postWebClient
.get()
.uri("/posts?limit={limit}&skip={skip}", limit, skip)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.bodyToMono(PostResult.class)
.block();

totalResult = result.total();

skip += limit;

allPosts.addAll(result.posts());

} while (allPosts.size() < totalResult);

return allPosts;
}
}
public class PostClient {}
5 changes: 0 additions & 5 deletions src/main/java/de/rieckpil/PostResult.java

This file was deleted.

12 changes: 0 additions & 12 deletions src/main/java/de/rieckpil/TimeProvider.java

This file was deleted.

43 changes: 1 addition & 42 deletions src/main/java/de/rieckpil/TimeUtil.java
Original file line number Diff line number Diff line change
@@ -1,50 +1,9 @@
package de.rieckpil;

import java.time.LocalDate;
import java.time.Period;

import org.springframework.stereotype.Component;

/**
* Develop a feature to display information when a comment was made (one day ago, 3 days ago, 6
* month ago, etc.) in a human-readable format: - A comment that is older than 365 days, should
* return 'more than a year'. - A comment within today should return 'today'. - A date in the future
* is invalid and should throw an exception.
*/
@Component
public class TimeUtil {

private final TimeProvider timeProvider;

public TimeUtil(TimeProvider timeProvider) {
this.timeProvider = timeProvider;
}

public String getDiffBetweenCreationDate(LocalDate creationDate) {

LocalDate currentDate = timeProvider.getCurrentDate();

Period periodBetween = Period.between(creationDate, currentDate);

if (periodBetween.isNegative()) {
throw new IllegalArgumentException("Creation date must not be in the future");
}

if (periodBetween.getYears() > 0) {
return "more than a year ago";
} else if (periodBetween.getMonths() > 0) {
return formatTimeAgo(periodBetween.getMonths(), "month");
} else if (periodBetween.getDays() > 0) {
return formatTimeAgo(periodBetween.getDays(), "day");
} else {
return "today";
}
}

private String formatTimeAgo(int amount, String unit) {
if (amount == 1) {
return "one " + unit + " ago";
}
return amount + " " + unit + "s ago";
}
}
public class TimeUtil {}
21 changes: 0 additions & 21 deletions src/main/java/de/rieckpil/WebClientConfiguration.java

This file was deleted.

32 changes: 0 additions & 32 deletions src/main/java/de/rieckpil/WebSecurityConfiguration.java

This file was deleted.

131 changes: 0 additions & 131 deletions src/test/java/de/rieckpil/CommentApiControllerTest.java

This file was deleted.

Loading

0 comments on commit 630876d

Please sign in to comment.