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

### VS Code ###
.vscode/

### personal ###
application.yml
Dockerfile
Binary file added 0.0.1.jar
Binary file not shown.
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: 'docker-day-2: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);
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/booleanuk/api/post/Post.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.booleanuk.api.post;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "posts")
public class Post {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column
private String text;

public Post(String text) {
this.text = text;
}
}
60 changes: 60 additions & 0 deletions src/main/java/com/booleanuk/api/post/PostController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.booleanuk.api.post;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

@RestController
@RequestMapping("posts")
public class PostController {

@Autowired
private PostRepository postRepository;

@ResponseStatus(HttpStatus.OK)
@GetMapping
public List<Post> getAllPosts() {
return this.postRepository.findAll();
}

@ResponseStatus(HttpStatus.OK)
@GetMapping("{id}")
public Post getOnePost(@PathVariable int id) {
return this.postRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Post not found.")
);
}

@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public Post createPost(@RequestBody Post post) {

Post newPost = new Post(post.getText());
return this.postRepository.save(newPost);
}

@ResponseStatus(HttpStatus.CREATED)
@PutMapping("{id}")
public Post updatePost(@PathVariable int id, @RequestBody Post post) {
Post postToUpdate = this.postRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Post not found")
);

postToUpdate.setText(post.getText());
return this.postRepository.save(postToUpdate);
}

@ResponseStatus(HttpStatus.OK)
@DeleteMapping("{id}")
public Post deletePost(@PathVariable int id) {
Post postToDelete = this.postRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Post not found.")
);

this.postRepository.delete(postToDelete);
return postToDelete;
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/booleanuk/api/post/PostRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.booleanuk.api.post;

import org.springframework.data.jpa.repository.JpaRepository;

public interface PostRepository extends JpaRepository<Post, Integer> {

}
27 changes: 27 additions & 0 deletions src/main/java/com/booleanuk/api/user/User.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.booleanuk.api.user;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column
private String name;

public User(String name) {
this.name = name;
}
}
60 changes: 60 additions & 0 deletions src/main/java/com/booleanuk/api/user/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.booleanuk.api.user;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

@RestController
@RequestMapping("users")
public class UserController {

@Autowired
private UserRepository userRepository;

@ResponseStatus(HttpStatus.OK)
@GetMapping
public List<User> getAllUsers() {
return this.userRepository.findAll();
}

@ResponseStatus(HttpStatus.OK)
@GetMapping("{id}")
public User getOneUser(@PathVariable int id) {
return this.userRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found.")
);
}

@ResponseStatus(HttpStatus.CREATED)
@PostMapping
public User createUser(@RequestBody User user) {

User newUser = new User(user.getName());
return this.userRepository.save(newUser);
}

@ResponseStatus(HttpStatus.CREATED)
@PutMapping("{id}")
public User updateUser(@PathVariable int id, @RequestBody User user) {
User userToUpdate = this.userRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found")
);

userToUpdate.setName(user.getName());
return this.userRepository.save(userToUpdate);
}

@ResponseStatus(HttpStatus.OK)
@DeleteMapping("{id}")
public User deleteUser(@PathVariable int id) {
User userToDelete = this.userRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found.")
);

this.userRepository.delete(userToDelete);
return userToDelete;
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/booleanuk/api/user/UserRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.booleanuk.api.user;

import org.springframework.data.jpa.repository.JpaRepository;

public interface UserRepository extends JpaRepository<User, Integer> {

}