Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,6 @@ out/

### VS Code ###
.vscode/

application.yml
*.jar
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM eclipse-temurin:18

WORKDIR /app

COPY java.docker.day.2-0.0.1.jar /app/java.docker.day.2-0.0.1.jar

EXPOSE 4000

ENTRYPOINT ["java", "-jar", "java.docker.day.2-0.0.1.jar"]
11 changes: 11 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ dependencies {
tasks.named('test') {
useJUnitPlatform()
}

jar {
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest {
attributes 'Main-Class': 'com.booleanuk.api.Main'
}

from {
configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
}
}
21 changes: 21 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
services:
app:
image: 'test-prog:latest'
container_name: app
depends_on:
- db
ports:
- '4000:4000'
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/mypostgresuser
- SPRING_DATASOURCE_USERNAME=mypostgresuser
- SPRING_DATASOURCE_PASSWORD=mypostgrespassword
- SPRING_JPA_HIBERNATE_DDL_AUTO=update

db:
image: 'postgres:latest'
container_name: db
environment:
- POSTGRES_USER=mypostgresuser
- POSTGRES_DATABASE=mypostgresuser
- POSTGRES_PASSWORD=mypostgrespassword
11 changes: 11 additions & 0 deletions src/main/java/com/booleanuk/api/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.booleanuk.api;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
221 changes: 221 additions & 0 deletions src/main/java/com/booleanuk/api/controller/PostController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
package com.booleanuk.api.controller;

import com.booleanuk.api.model.ApiResponse;
import com.booleanuk.api.model.Like;
import com.booleanuk.api.model.Post;
import com.booleanuk.api.model.User;
import com.booleanuk.api.repository.LikeRepository;
import com.booleanuk.api.repository.PostRepository;
import com.booleanuk.api.repository.UserRepository;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("post")
public class PostController {
@Autowired
private final PostRepository postRepository;

@Autowired
private final UserRepository userRepository;

@Autowired
private final LikeRepository likeRepository;

public PostController(PostRepository postRepository, UserRepository userRepository, LikeRepository likeRepository) {
this.postRepository = postRepository;
this.userRepository = userRepository;
this.likeRepository = likeRepository;
}

@PostMapping("{userId}")
public ResponseEntity<ApiResponse<?>> createPost (@PathVariable int userId, @RequestBody Post postDetails) {
Optional<User> use = this.userRepository.findById(userId);

if(use.isEmpty()) {
ApiResponse<String> response = new ApiResponse<>("error", "Could not find user with id " + userId);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
User user = use.get();

if(postDetails.isInvalid()) {
ApiResponse<String> response = new ApiResponse<>("error", "Could not create a post with the specified parameters.");
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}

Post post = new Post(postDetails.getContents(), user, null);
user.getPosts().add(post);

this.postRepository.save(post);
this.userRepository.save(user);

ApiResponse<Post> response = new ApiResponse<>("success", post);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}

@PostMapping("{userId}/{postId}")
public ResponseEntity<ApiResponse<?>> createComment (@PathVariable int userId, @PathVariable int postId, @RequestBody Post postDetails) {
Optional<User> use = this.userRepository.findById(userId);

if(use.isEmpty()) {
ApiResponse<String> response = new ApiResponse<>("error", "Could not find user with id " + userId);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
User user = use.get();

Optional<Post> pos = this.postRepository.findById(postId);
if(pos.isEmpty()) {
ApiResponse<String> response = new ApiResponse<>("error", "Could not find post with id " + userId);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
Post post = pos.get();

if(postDetails.isInvalid()) {
ApiResponse<String> response = new ApiResponse<>("error", "Could not create a post with the specified parameters.");
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}

Post comment = new Post(postDetails.getContents(), user, post);

user.getPosts().add(comment);
post.getComments().add(comment);

this.postRepository.save(comment);
this.postRepository.save(post);
this.userRepository.save(user);

ApiResponse<Post> response = new ApiResponse<>("success", comment);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}

@PostMapping("{postId}/like/{userId}")
public ResponseEntity<ApiResponse<?>> createLike (@PathVariable int userId, @PathVariable int postId) {
Optional<User> use = this.userRepository.findById(userId);
if(use.isEmpty()) {
ApiResponse<String> response = new ApiResponse<>("error", "Could not find user with id " + userId);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
User user = use.get();

Optional<Post> pos = this.postRepository.findById(postId);
if(pos.isEmpty()) {
ApiResponse<String> response = new ApiResponse<>("error", "Could not find post with id " + userId);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
Post post = pos.get();

Like like = new Like(user, post);
user.getLikes().add(like);
post.getLikes().add(like);

this.likeRepository.save(like);
this.postRepository.save(post);
this.userRepository.save(user);

ApiResponse<Like> response = new ApiResponse<>("success", like);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}

@PostMapping("{postId}/repost/{userId}")
public ResponseEntity<ApiResponse<?>> repost (@PathVariable int userId, @PathVariable int postId) {
Optional<User> use = this.userRepository.findById(userId);
if(use.isEmpty()) {
ApiResponse<String> response = new ApiResponse<>("error", "Could not find user with id " + userId);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
User user = use.get();

Optional<Post> pos = this.postRepository.findById(postId);
if(pos.isEmpty()) {
ApiResponse<String> response = new ApiResponse<>("error", "Could not find post with id " + userId);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
Post post = pos.get();

user.getPosts().add(post);
this.userRepository.save(user);

ApiResponse<Post> response = new ApiResponse<>("success", post);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}

@GetMapping
public ResponseEntity<ApiResponse<List<Post>>> readAll() {
List<Post> posts = this.postRepository.findAll();
ApiResponse<List<Post>> response = new ApiResponse<>("success", posts);
return new ResponseEntity<>(response, HttpStatus.OK);
}

@GetMapping("{id}")
public ResponseEntity<ApiResponse<?>> readOne (@PathVariable int id) {
Optional<Post> pos = this.postRepository.findById(id);

if(pos.isEmpty()) {
ApiResponse<String> response = new ApiResponse<>("error", "Could not find post with id " + id);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}

Post post = pos.get();
ApiResponse<Post> response = new ApiResponse<>("success", post);
return new ResponseEntity<>(response, HttpStatus.OK);
}

@PutMapping("{id}")
public ResponseEntity<ApiResponse<?>> update (@PathVariable int id, @RequestBody Post postDetails) {
Optional<Post> pos = this.postRepository.findById(id);

if(pos.isEmpty()) {
ApiResponse<String> response = new ApiResponse<>("error", "Could not find post with id " + id);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}

Post post = pos.get();

if(postDetails.isInvalid()) {
ApiResponse<String> response = new ApiResponse<>("error", "Could not create a post with the specified parameters.");
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}

post.setContents(postDetails.getContents());

ApiResponse<Post> response = new ApiResponse<>("success", post);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}

@DeleteMapping("{id}")
public ResponseEntity<ApiResponse<?>> delete (@PathVariable int id) {
Optional<Post> pos = this.postRepository.findById(id);

if(pos.isEmpty()) {
ApiResponse<String> response = new ApiResponse<>("error", "Could not find post with id " + id);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}

Post post = pos.get();
this.postRepository.delete(post);

ApiResponse<Post> response = new ApiResponse<>("success", post);
return new ResponseEntity<>(response, HttpStatus.OK);
}

@DeleteMapping("like/{id}")
public ResponseEntity<ApiResponse<?>> deleteLike (@PathVariable int id) {
Optional<Like> lik = this.likeRepository.findById(id);

if(lik.isEmpty()) {
ApiResponse<String> response = new ApiResponse<>("error", "Could not find like with id " + id);
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}

Like like = lik.get();

ApiResponse<Like> response = new ApiResponse<>("success", like);
return new ResponseEntity<>(response, HttpStatus.OK);
}
}
Loading