Skip to content
Open

done #72

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
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM openjdk:17-jdk-slim

WORKDIR /app

COPY build/libs/*.jar app.jar

EXPOSE 4000

CMD ["java", "-jar", "app.jar"]
35 changes: 35 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
version: '3.8'

services:
app:
build: .
ports:
- "4001:4000"
depends_on:
- db
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/jokedb
- SPRING_DATASOURCE_USERNAME=user
- SPRING_DATASOURCE_PASSWORD=password
networks:
- app-network

db:
image: postgres:15
environment:
- POSTGRES_DB=jokedb
- POSTGRES_USER=user
- POSTGRES_PASSWORD=password
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
networks:
- app-network

volumes:
postgres_data:

networks:
app-network:
driver: bridge
50 changes: 50 additions & 0 deletions src/main/java/com/booleanuk/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.booleanuk;

import com.booleanuk.model.Joke;
import com.booleanuk.repository.JokeRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class Main implements CommandLineRunner {
private final JokeRepository jokeRepository;

public Main(JokeRepository jokeRepository) {
this.jokeRepository = jokeRepository;
}

public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}


@Override
public void run(String... args) throws Exception {
List<Joke> jokes = new ArrayList<>();
if(this.jokeRepository.findAll().isEmpty()) {
jokes.add(new Joke("V2h5IHdhcyB0aGUgbXVzaWNpYW4gYXJyZXN0ZWQ/IEhlIGdvdCBpbiB0cmVibGUu"));
jokes.add(new Joke("SGUgd2FzIGEgcmVhbCBnZW50bGVtZW4gYW5kIGFsd2F5cyBvcGVuZWQgdGhlIGZyaWRnZSBkb29yIGZvciBtZQ=="));
jokes.add(new Joke("V2hhdCBzaG91bGQgeW91IGRvIGJlZm9yZSBjcml0aWNpemluZyBQYWMtTWFuPyBXQUtBIFdBS0EgV0FLQSBtaWxlIGluIGhpcyBzaG9lcw=="));
jokes.add(new Joke("V2h5IGRvIHlvdSBuZXZlciBzZWUgZWxlcGhhbnRzIGhpZGluZyBpbiB0cmVlcz8gJ0NhdXNlIHRoZXkgYXJlIGZyZWFraW5nIGdvb2QgYXQgaXQ="));
jokes.add(new Joke("V2hhdCBkbyB5b3UgY2FsbCBhIHBvdGF0byBpbiBzcGFjZT8gU3B1ZG5paw=="));
jokes.add(new Joke("V2h5IGRpZCB0aGUgY2hpY2tlbiBob2xkIGEgc2VhbmNlPyBUbyBnZXQgdG8gdGhlIG90aGVyIHNpZGUu"));
jokes.add(new Joke("V2hlcmUgZG8gYmFieSBjb3dzIGdvIHRvIGVhdCBsdW5jaD8gQXQgdGhlIGNhbGYtZXRlcmlhLg=="));
jokes.add(new Joke("SSBhbSBsb29raW5nIGZvcndhcmQgdG8gNnBtIFRoYW5rc2dpdmluZyBEYXkgd2hlbiBXYWxtYXJ0IG9wZW5zIGl0cyBkb29ycyBmb3IgaXRzIGFubnVhbCBzYWxlIG9mIHRyYW1wbGVkIGh1bWFuIGNvcnBzZXMu"));
jokes.add(new Joke("V2hhdCBkaWQgdGhlIGNhciBzYWlkIHRvIHRoZSB2YWxldD8gSSd2ZSBiZWVuIHRocm91Z2ggYSBsb3Qu"));
jokes.add(new Joke("SSB3YXMgZHJpbmtpbmcgYXQgdGhlIGJhciwgc28gSSB0b29rIGEgYnVzIGhvbWUuIFRoYXQgbWF5IG5vdCBiZSBhIGJpZyBkZWFsIHRvIHlvdSwgYnV0IEkndmUgbmV2ZXIgZHJpdmVuIGEgYnVzIGJlZm9yZSE="));
jokes.add(new Joke("V2hhdCBkbyB5b3UgY2FsbCBhIGJsaW5kIGRpbm9zYXVyPyBBIGRvLXRoaW5rLWhlLXNhdXJ1cw=="));
jokes.add(new Joke("TXkgZXgtd2lmZSBzdGlsbCBtaXNzZXMgbWUuLi4gQnV0IGhlciBhaW0gaXMgZ2V0dGluIGJldHRlci4="));
jokes.add(new Joke("VHdvIHNrZXB0aWNzIHdhbGsgaW50byBhIGJhci4uIEknZCB0ZWxsIHlvdSB3aGF0IGhhcHBlbnMgbmV4dCBidXQgbm9vbmUga25vd3M="));
jokes.add(new Joke("Kkd1eSB0cmllcyBnaXZpbmcgbWUgaGlzIHBob25lIG51bWJlciogTWU6IE9oIG5vIHRoYW5rIHlvdS4gSSBhbHJlYWR5IGhhdmUgb25l"));
jokes.add(new Joke("V2h5IGNhbid0IGEgYmlrZSBzdGFuZCBvbiBpdCdzIG93bj8gQmVjYXVzZSBpdCBpcyB0d28gdGlyZWQu"));
jokes.add(new Joke("TXkgaG91c2UgaXMgcmVhbGx5IHNtYWxsIHVudGlsIEkgY2FuJ3QgZmluZCBteSBwaG9uZS4="));
jokes.add(new Joke("SG93IGRvIHRoZXkgY2FsY3VsYXRlIGdsb2JhbCB3YXJtaW5nPyBBbC1nb3JlLXl0aG1z"));
jokes.add(new Joke("V2h5IGRpZCB0aGUgY29tcG9zZXIgZ28gdG8gdGhlIGNoaXJvcHJhY3Rvcj8gQmVjYXVzZSBoZSBoYWQgQmFjaCBwcm9ibGVtcw=="));
jokes.add(new Joke("QSBzaGVlcCwgYSBkcnVtIGFuZCBhIHNuYWtlIGZhbGwgZG93biBhIGNsaWZmLCBiYSBkdW0gdHNz"));
jokes.forEach(joke -> {jokeRepository.save(joke);});}
}
}
70 changes: 70 additions & 0 deletions src/main/java/com/booleanuk/controller/JokeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.booleanuk.controller;

import com.booleanuk.model.Joke;
import com.booleanuk.repository.JokeRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.Base64;
import java.util.List;

@RestController
@RequestMapping("jokes")
public class JokeController {
@Autowired
private JokeRepository jokeRepository;

private Base64 Base64;

public JokeController(JokeRepository jokeRepository) {
this.jokeRepository = jokeRepository;
}

record PostBook(String jokeText) {};

@GetMapping
public List<Joke> getBooks() {
return jokeRepository.findAll();
}

@PostMapping
public ResponseEntity<Joke> create(@RequestBody PostBook request){
Joke joke = new Joke(Base64.getEncoder().encodeToString((request.jokeText).getBytes()));
return new ResponseEntity<>(jokeRepository.save(joke), HttpStatus.CREATED);
}

@GetMapping("/{id}")
public ResponseEntity<Joke> getById(@PathVariable("id") Integer id){
Joke joke = jokeRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Joke not found")
);

byte[] actualByte= Base64.getDecoder().decode(joke.getJokeText().getBytes());
joke.setJokeText(new String(actualByte));

return ResponseEntity.ok(joke);
}

@PutMapping("/{id}")
public ResponseEntity<Joke> update(@PathVariable int id, @RequestBody PostBook request){
Joke joke = this.jokeRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Joke not found")
);

joke.setJokeText(request.jokeText);

return new ResponseEntity<>(jokeRepository.save(joke), HttpStatus.CREATED);
}

@DeleteMapping("/{id}")
public ResponseEntity<Joke> delete(@PathVariable int id){
Joke joke = this.jokeRepository.findById(id).orElseThrow(
() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Joke not found")
);
this.jokeRepository.delete(joke);
return new ResponseEntity<>(joke, HttpStatus.OK);
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/booleanuk/model/Joke.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.booleanuk.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(name = "jokes")
@Getter
@Setter
@NoArgsConstructor
public class Joke {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column
private String jokeText;


public Joke(String jokeText) {
this.jokeText = jokeText;
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/booleanuk/repository/JokeRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.booleanuk.repository;

import com.booleanuk.model.Joke;
import org.springframework.data.jpa.repository.JpaRepository;

public interface JokeRepository extends JpaRepository<Joke, Integer> {
}
7 changes: 0 additions & 7 deletions src/main/resources/application.yml

This file was deleted.