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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ out/

### VS Code ###
.vscode/

application.yml
9 changes: 9 additions & 0 deletions Docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM mcr.microsoft.com/openjdk/jdk:21-ubuntu

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" ]
23 changes: 23 additions & 0 deletions Docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
services:
app:
image: "blogging-box: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
ports:
- "5432:5432"
environment:
- POSTGRES_USER=mypostgresuser
- POSTGRES_DATABASE=mypostgresuser
- POSTGRES_PASSWORD=mypostgrespassword
17 changes: 14 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.1.4'
id 'io.spring.dependency-management' version '1.1.3'
id 'org.springframework.boot' version '3.4.1'
id 'io.spring.dependency-management' version '1.1.7'
}

group = 'com.booleanuk'
Expand All @@ -23,11 +23,22 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
implementation 'org.postgresql:postgresql:42.6.0'
runtimeOnly 'org.postgresql:postgresql'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

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) }
}
}
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);
}
}
155 changes: 155 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,155 @@
package com.booleanuk.api.controller;

import com.booleanuk.api.model.Comment;
import com.booleanuk.api.model.Post;
import com.booleanuk.api.repository.CommentRepository;
import com.booleanuk.api.repository.PostRepository;
import com.booleanuk.api.repository.UserRepository;
import com.booleanuk.api.response.*;
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.time.LocalDateTime;

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

@Autowired
private UserRepository userRepository;

@Autowired
private CommentRepository commentRepository;

private ErrorResponse errorResponse = new ErrorResponse();
private PostResponse postResponse = new PostResponse();
private PostListResponse postListResponse = new PostListResponse();
private CommentListResponse commentListResponse = new CommentListResponse();
private CommentResponse commentResponse = new CommentResponse();
private RepostListResponse repostListResponse = new RepostListResponse();

@GetMapping
public ResponseEntity<Response<?>> getAllPosts() {
postListResponse.set(this.postRepository.findAll());
return ResponseEntity.ok(postListResponse);
}

@GetMapping("{id}/reposts")
public ResponseEntity<Response<?>> getAllRepostsForPost(@PathVariable int id) {
Post postWithReposts = this.postRepository.findById(id).orElse(null);
if (postWithReposts == null) {
this.errorResponse.set("Not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
this.repostListResponse.set(postWithReposts.getReposts());
return ResponseEntity.ok(repostListResponse);
}

@PostMapping
public ResponseEntity<Response<?>> createPost(@RequestBody Post post) {
post.setPosted(LocalDateTime.now());
post.setUpdated(LocalDateTime.now());
try {
postResponse.set(this.postRepository.save(post));
} catch (Exception e) {
errorResponse.set("Bad request");
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(postResponse, HttpStatus.CREATED);
}

@GetMapping("{id}")
public ResponseEntity<Response<?>> getOnePost(@PathVariable int id) {
Post post = this.postRepository.findById(id).orElse(null);
if (post == null) {
this.errorResponse.set("Not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
this.postResponse.set(post);
return ResponseEntity.ok(postResponse);
}

@PutMapping("{id}")
public ResponseEntity<Response<?>> updatePost(@PathVariable int id, @RequestBody Post post) {
Post postToUpdate = this.postRepository.findById(id).orElse(null);
if (postToUpdate == null) {
this.errorResponse.set("Not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
postToUpdate.setTitle(post.getTitle());
postToUpdate.setContent(post.getContent());
postToUpdate.setUpdated(LocalDateTime.now());
try {
postToUpdate = this.postRepository.save(postToUpdate);
} catch (Exception e) {
this.errorResponse.set("Bad request");
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}
this.postResponse.set(postToUpdate);
return new ResponseEntity<>(postResponse, HttpStatus.CREATED);
}

@DeleteMapping("{id}")
public ResponseEntity<Response<?>> deletePost(@PathVariable int id) {
Post postToDelete = this.postRepository.findById(id).orElse(null);
if (postToDelete == null) {
this.errorResponse.set("Not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
this.postRepository.delete(postToDelete);
postResponse.set(postToDelete);
return ResponseEntity.ok(postResponse);
}

@GetMapping("{id}/comments")
public ResponseEntity<Response<?>> getAllComments(@PathVariable int id) {
Post postWithComments = this.postRepository.findById(id).orElse(null);
if (postWithComments == null) {
errorResponse.set("Not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
this.commentListResponse.set(postWithComments.getComments());
return ResponseEntity.ok(commentListResponse);
}

@PostMapping("{id}/comments")
public ResponseEntity<Response<?>> addComment(@PathVariable int id, @RequestBody Comment comment) {
Post postWithComments = this.postRepository.findById(id).orElse(null);
if (postWithComments == null) {
this.errorResponse.set("Not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
postWithComments.getComments().add(comment);
this.commentResponse.set(comment);
return ResponseEntity.ok(commentResponse);
}

@GetMapping("{post_id}/comments/{comment_id}")
public ResponseEntity<Response<?>> getOneComment(@PathVariable int postId, @PathVariable int commentId) {
Post post = this.postRepository.findById(postId).orElse(null);
Comment comment = this.commentRepository.findById(commentId).orElse(null);
if (post == null || comment == null) {
this.errorResponse.set("Post or comment not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
this.commentResponse.set(comment);
return ResponseEntity.ok(commentResponse);
}

@DeleteMapping("{post_id}/friends/{comment_id}")
public ResponseEntity<Response<?>> removeComment(@PathVariable int postId, @PathVariable int commentId) {
Post post = this.postRepository.findById(postId).orElse(null);
Comment comment = this.commentRepository.findById(commentId).orElse(null);
if (post == null || comment == null) {
this.errorResponse.set("Post or comment not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
post.getComments().remove(comment);
this.commentResponse.set(comment);
return ResponseEntity.ok(commentResponse);
}
}
68 changes: 68 additions & 0 deletions src/main/java/com/booleanuk/api/controller/RepostController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.booleanuk.api.controller;

import com.booleanuk.api.model.Post;
import com.booleanuk.api.model.Repost;
import com.booleanuk.api.repository.PostRepository;
import com.booleanuk.api.repository.RepostRepository;
import com.booleanuk.api.response.*;
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.time.LocalDateTime;

@RestController
@RequestMapping("reposts")
public class RepostController {
@Autowired
private RepostRepository repostRepository;

@Autowired
private PostRepository postRepository;

private ErrorResponse errorResponse = new ErrorResponse();
private RepostResponse repostResponse = new RepostResponse();
private RepostListResponse repostListResponse = new RepostListResponse();

@GetMapping
public ResponseEntity<Response<?>> getAllReposts() {
this.repostListResponse.set(this.repostRepository.findAll());
return ResponseEntity.ok(repostListResponse);
}

@PostMapping
public ResponseEntity<Response<?>> createRepost(@RequestBody Repost repost) {
repost.setReposted(LocalDateTime.now());
try {
this.repostResponse.set(this.repostRepository.save(repost));
} catch (Exception e) {
this.errorResponse.set("Bad request");
return new ResponseEntity<>(errorResponse, HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<>(repostResponse, HttpStatus.CREATED);
}

@GetMapping("{id}")
public ResponseEntity<Response<?>> getOneRepost(@PathVariable int id) {
Repost repost = this.repostRepository.findById(id).orElse(null);
if (repost == null) {
this.errorResponse.set("Not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
this.repostResponse.set(repost);
return ResponseEntity.ok(repostResponse);
}

@DeleteMapping("{id}")
public ResponseEntity<Response<?>> deleteRepost(@PathVariable int id) {
Repost repostToDelete = this.repostRepository.findById(id).orElse(null);
if (repostToDelete == null) {
this.errorResponse.set("Not found");
return new ResponseEntity<>(errorResponse, HttpStatus.NOT_FOUND);
}
this.repostRepository.delete(repostToDelete);
this.repostResponse.set(repostToDelete);
return ResponseEntity.ok(repostResponse);
}
}
Loading