simple paste create api
This commit is contained in:
13
pom.xml
13
pom.xml
@@ -42,7 +42,6 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
@@ -53,6 +52,18 @@
|
|||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.postgresql</groupId>
|
||||||
|
<artifactId>postgresql</artifactId>
|
||||||
|
<version>42.7.7</version>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>3.18.0</version>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
package id.my.luxic.pastebin.controller;
|
||||||
|
|
||||||
|
import id.my.luxic.pastebin.model.Paste;
|
||||||
|
import id.my.luxic.pastebin.request.PasteCreateRequest;
|
||||||
|
import id.my.luxic.pastebin.service.PasteService;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class PasteController {
|
||||||
|
|
||||||
|
private final PasteService service;
|
||||||
|
|
||||||
|
public PasteController(PasteService service) {
|
||||||
|
this.service = service;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/api/paste/findAll")
|
||||||
|
public ResponseEntity<List<Paste>> findAll() {
|
||||||
|
return ResponseEntity.ok(service.findAll());
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/api/paste/create")
|
||||||
|
public ResponseEntity<Paste> create(@RequestBody PasteCreateRequest pasteCreateRequest) {
|
||||||
|
return ResponseEntity.ok(service.save(pasteCreateRequest));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
29
src/main/java/id/my/luxic/pastebin/model/Paste.java
Normal file
29
src/main/java/id/my/luxic/pastebin/model/Paste.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package id.my.luxic.pastebin.model;
|
||||||
|
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@Builder
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
@Table(name = "paste")
|
||||||
|
public class Paste {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
private String id;
|
||||||
|
private String content;
|
||||||
|
private String title;
|
||||||
|
private LocalDateTime expireAt;
|
||||||
|
private LocalDateTime created;
|
||||||
|
private LocalDateTime updated;
|
||||||
|
private Boolean isPrivate;
|
||||||
|
private Boolean isDeleted;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
package id.my.luxic.pastebin.repository;
|
||||||
|
|
||||||
|
import id.my.luxic.pastebin.model.Paste;
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface PasteRepository extends JpaRepository<Paste, String> {
|
||||||
|
}
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
package id.my.luxic.pastebin.request;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
import java.sql.Timestamp;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class PasteCreateRequest {
|
||||||
|
|
||||||
|
private String content;
|
||||||
|
private String title;
|
||||||
|
private Boolean isPrivate;
|
||||||
|
private Expiration expiration;
|
||||||
|
|
||||||
|
public enum Expiration {
|
||||||
|
NEVER {
|
||||||
|
@Override
|
||||||
|
public LocalDateTime getTimestamp() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
ONE_HOUR {
|
||||||
|
@Override
|
||||||
|
public LocalDateTime getTimestamp() {
|
||||||
|
return LocalDateTime.now().plusHours(1);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
ONE_DAY {
|
||||||
|
@Override
|
||||||
|
public LocalDateTime getTimestamp() {
|
||||||
|
return LocalDateTime.now().plusDays(1);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
ONE_WEEK {
|
||||||
|
@Override
|
||||||
|
public LocalDateTime getTimestamp() {
|
||||||
|
return LocalDateTime.now().plusWeeks(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public abstract LocalDateTime getTimestamp();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
18
src/main/java/id/my/luxic/pastebin/service/PasteService.java
Normal file
18
src/main/java/id/my/luxic/pastebin/service/PasteService.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package id.my.luxic.pastebin.service;
|
||||||
|
|
||||||
|
import id.my.luxic.pastebin.model.Paste;
|
||||||
|
import id.my.luxic.pastebin.request.PasteCreateRequest;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface PasteService {
|
||||||
|
|
||||||
|
Paste findById(String id);
|
||||||
|
|
||||||
|
List<Paste> findAll();
|
||||||
|
|
||||||
|
Paste save(PasteCreateRequest pasteCreateRequest);
|
||||||
|
|
||||||
|
void delete(String id);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
package id.my.luxic.pastebin.service.impl;
|
||||||
|
|
||||||
|
import id.my.luxic.pastebin.model.Paste;
|
||||||
|
import id.my.luxic.pastebin.repository.PasteRepository;
|
||||||
|
import id.my.luxic.pastebin.request.PasteCreateRequest;
|
||||||
|
import id.my.luxic.pastebin.service.PasteService;
|
||||||
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class PasteServiceImpl implements PasteService {
|
||||||
|
|
||||||
|
private final PasteRepository repository;
|
||||||
|
|
||||||
|
public PasteServiceImpl(PasteRepository repository) {
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Paste findById(String id) {
|
||||||
|
return repository.findById(id).orElse(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Paste> findAll() {
|
||||||
|
return repository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Paste save(PasteCreateRequest pasteCreateRequest) {
|
||||||
|
|
||||||
|
Paste paste = Paste.builder()
|
||||||
|
.id(RandomStringUtils.secure().nextAlphanumeric(10))
|
||||||
|
.title(pasteCreateRequest.getTitle())
|
||||||
|
.content(pasteCreateRequest.getContent())
|
||||||
|
.isPrivate(pasteCreateRequest.getIsPrivate())
|
||||||
|
.expireAt(pasteCreateRequest.getExpiration().getTimestamp())
|
||||||
|
.created(LocalDateTime.now())
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return repository.save(paste);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delete(String id) {
|
||||||
|
repository.deleteById(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1 +1,7 @@
|
|||||||
spring.application.name=pastebin
|
spring.application.name=pastebin
|
||||||
|
|
||||||
|
spring.datasource.url=jdbc:postgresql://localhost:5432/pastebin
|
||||||
|
spring.datasource.username=postgres
|
||||||
|
spring.datasource.password=password
|
||||||
|
|
||||||
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
Reference in New Issue
Block a user