-
Notifications
You must be signed in to change notification settings - Fork 0
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
0 parents
commit fc0d23c
Showing
15 changed files
with
265 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
### in terminal: | ||
docker-compose -f <file directory>/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} |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
services: | ||
db: | ||
image: postgres | ||
restart: on-failure | ||
environment: | ||
POSTGRES_PASSWORD: admin | ||
POSTGRES_USER: admin | ||
volumes: | ||
- <local directory>:/data/postgres | ||
ports: | ||
- 5432:5432 |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>3.0.1</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<groupId>com.example</groupId> | ||
<artifactId>demo</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>demo</name> | ||
<description>Demo project for Spring Boot</description> | ||
<properties> | ||
<java.version>17</java.version> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.postgresql</groupId> | ||
<artifactId>postgresql</artifactId> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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 |
---|---|---|
@@ -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<Note> getNotes(){ | ||
return noteRepository.findAll(); | ||
} | ||
|
||
@GetMapping("/") | ||
public String greetHome(){ | ||
return "GET request should be made to /api/"; | ||
} | ||
} |
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package com.example.demo; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface NoteRepository extends JpaRepository<Note, Integer> { | ||
} |
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Manifest-Version: 1.0 | ||
Main-Class: Main.java | ||
|
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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() { | ||
} | ||
|
||
} |