-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
3 additions
and
533 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
131 changes: 0 additions & 131 deletions
131
src/test/java/de/rieckpil/CommentApiControllerTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.