simple paste create api

This commit is contained in:
luxic
2025-08-17 09:12:15 +07:00
parent 1f1a5e0254
commit bdfd8a9142
8 changed files with 203 additions and 1 deletions

13
pom.xml
View File

@@ -42,7 +42,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
@@ -53,6 +52,18 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
<build>

View File

@@ -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));
}
}

View 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;
}

View File

@@ -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> {
}

View File

@@ -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();
}
}

View 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);
}

View File

@@ -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);
}
}

View File

@@ -1 +1,7 @@
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