diff --git a/README.md b/README.md new file mode 100644 index 0000000..0194187 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +### in terminal: +docker-compose -f /docker-compose.yml up -d +docker exec -it x-db-1 psql -U admin \l +CREATE DATABASE notes; + +### start the java app + +### GET/POST/DELETE +get: localhost:8050/api/db +post: localhost:8050/api/db +delete: localhost:8050/api/db/{id} \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c870cf7 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +services: + db: + image: postgres + restart: on-failure + environment: + POSTGRES_PASSWORD: admin + POSTGRES_USER: admin + volumes: + - :/data/postgres + ports: + - 5432:5432 \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..30cb4f4 --- /dev/null +++ b/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 3.0.1 + + + com.example + demo + 0.0.1-SNAPSHOT + demo + Demo project for Spring Boot + + 17 + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + org.springframework.boot + spring-boot-starter-web + + + + org.postgresql + postgresql + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000..5fa1f92 Binary files /dev/null and b/src/.DS_Store differ diff --git a/src/main/.DS_Store b/src/main/.DS_Store new file mode 100644 index 0000000..6ebde77 Binary files /dev/null and b/src/main/.DS_Store differ diff --git a/src/main/java/.DS_Store b/src/main/java/.DS_Store new file mode 100644 index 0000000..35a54bf Binary files /dev/null and b/src/main/java/.DS_Store differ diff --git a/src/main/java/com/.DS_Store b/src/main/java/com/.DS_Store new file mode 100644 index 0000000..c85c3ab Binary files /dev/null and b/src/main/java/com/.DS_Store differ diff --git a/src/main/java/com/example/.DS_Store b/src/main/java/com/example/.DS_Store new file mode 100644 index 0000000..8067953 Binary files /dev/null and b/src/main/java/com/example/.DS_Store differ diff --git a/src/main/java/com/example/demo/Main.java b/src/main/java/com/example/demo/Main.java new file mode 100644 index 0000000..5e14c3f --- /dev/null +++ b/src/main/java/com/example/demo/Main.java @@ -0,0 +1,54 @@ +package com.example.demo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.web.bind.annotation.*; + +import java.time.LocalDate; +import java.util.List; + +@SpringBootApplication +@RestController +public class Main { + private final NoteRepository noteRepository; + + public Main(NoteRepository noteRepository) { + this.noteRepository = noteRepository; + } + + public static void main(String[] args){ + SpringApplication.run(Main.class, args); + } + + @DeleteMapping("/api/db/{noteId}") + public void deleteNote(@PathVariable("noteId") Integer id){ + noteRepository.deleteById(id); + } + + @PostMapping ("/api/db") + public void postNote(@RequestBody NewNoteRequest request){ + Note note = new Note(); + note.setTitle(request.title()); + note.setRating(request.rating()); + note.setBody(request.body()); + note.setDate(LocalDate.now()); + noteRepository.save(note); + } + + record NewNoteRequest( + String title, + String body, + Integer rating + + ){} + + @GetMapping("/api/db") + public List getNotes(){ + return noteRepository.findAll(); + } + + @GetMapping("/") + public String greetHome(){ + return "GET request should be made to /api/"; + } +} diff --git a/src/main/java/com/example/demo/Note.java b/src/main/java/com/example/demo/Note.java new file mode 100644 index 0000000..2c2f0db --- /dev/null +++ b/src/main/java/com/example/demo/Note.java @@ -0,0 +1,99 @@ +package com.example.demo; + +import jakarta.persistence.*; + +import java.time.LocalDate; + +@Entity +public class Note { + @Id + @SequenceGenerator( + name = "note_id_sequence", + sequenceName = "note_id_sequence" + ) + @GeneratedValue( + strategy = GenerationType.SEQUENCE, + generator = "note_id_sequence" + ) + private Integer id; + private String title; + private String body; + private LocalDate date; + private Integer rating; + + public Note(Integer id, String title, String body, Integer rating, LocalDate date) { + this.id = id; + this.title = title; + this.body = body; + this.rating = rating; + this.date = date; + } + + public Note() { + + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getBody() { + return body; + } + + public void setBody(String content) { + this.body = content; + } + + public LocalDate getDate() { + return date; + } + + public void setDate(LocalDate date) { + this.date = date; + } + + public Integer getRating() { + return rating; + } + + public void setRating(Integer rating) { + this.rating = rating; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Note note = (Note) o; + + if (id != note.id) return false; + if (rating != note.rating) return false; + if (!title.equals(note.title)) return false; + if (!body.equals(note.body)) return false; + return date.equals(note.date); + } + + @Override + public int hashCode() { + Integer result = id; + result = 31 * result + title.hashCode(); + result = 31 * result + body.hashCode(); + result = 31 * result + date.hashCode(); + result = 31 * result + rating; + return result; + } +} diff --git a/src/main/java/com/example/demo/NoteRepository.java b/src/main/java/com/example/demo/NoteRepository.java new file mode 100644 index 0000000..10536ac --- /dev/null +++ b/src/main/java/com/example/demo/NoteRepository.java @@ -0,0 +1,6 @@ +package com.example.demo; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface NoteRepository extends JpaRepository { +} diff --git a/src/main/resources/.DS_Store b/src/main/resources/.DS_Store new file mode 100644 index 0000000..2a43b86 Binary files /dev/null and b/src/main/resources/.DS_Store differ diff --git a/src/main/resources/META-INF/MANIFEST.MF b/src/main/resources/META-INF/MANIFEST.MF new file mode 100644 index 0000000..7b1f457 --- /dev/null +++ b/src/main/resources/META-INF/MANIFEST.MF @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +Main-Class: Main.java + diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..1bec9cb --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,18 @@ + server: + port: 8050 + + spring: + main: + web-application-type: servlet + datasource: + url: jdbc:postgresql://localhost:5432/notes + username: admin + password: admin + jpa: + hibernate: + ddl-auto: create-drop + properties: + hibernate: + dialect: org.hibernate.dialect.PostgreSQLDialect + format_sql: true + show-sql: true \ No newline at end of file diff --git a/src/test/java/com/example/demo/DemoApplicationTests.java b/src/test/java/com/example/demo/DemoApplicationTests.java new file mode 100644 index 0000000..2778a6a --- /dev/null +++ b/src/test/java/com/example/demo/DemoApplicationTests.java @@ -0,0 +1,13 @@ +package com.example.demo; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class DemoApplicationTests { + + @Test + void contextLoads() { + } + +}