Skip to content

Commit

Permalink
backend + sql
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisxadvocatus committed Jan 5, 2023
0 parents commit fc0d23c
Show file tree
Hide file tree
Showing 15 changed files with 265 additions and 0 deletions.
11 changes: 11 additions & 0 deletions README.md
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}
11 changes: 11 additions & 0 deletions docker-compose.yml
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
50 changes: 50 additions & 0 deletions pom.xml
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 added src/.DS_Store
Binary file not shown.
Binary file added src/main/.DS_Store
Binary file not shown.
Binary file added src/main/java/.DS_Store
Binary file not shown.
Binary file added src/main/java/com/.DS_Store
Binary file not shown.
Binary file added src/main/java/com/example/.DS_Store
Binary file not shown.
54 changes: 54 additions & 0 deletions src/main/java/com/example/demo/Main.java
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/";
}
}
99 changes: 99 additions & 0 deletions src/main/java/com/example/demo/Note.java
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;
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/example/demo/NoteRepository.java
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 added src/main/resources/.DS_Store
Binary file not shown.
3 changes: 3 additions & 0 deletions src/main/resources/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: Main.java

18 changes: 18 additions & 0 deletions src/main/resources/application.yml
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
13 changes: 13 additions & 0 deletions src/test/java/com/example/demo/DemoApplicationTests.java
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() {
}

}

0 comments on commit fc0d23c

Please sign in to comment.