add: pastes entity, controller and service

This commit is contained in:
Jose
2026-03-05 18:37:49 +01:00
parent 6949d8e083
commit 39d75aecf9
3 changed files with 10 additions and 4 deletions

View File

@@ -36,14 +36,17 @@ public class PasteController {
} }
@GetMapping("/{paste_key}") @GetMapping("/{paste_key}")
public ResponseEntity<PasteDto.Response> getByKey(@PathVariable("paste_key") String pasteKey, @RequestBody PastePassword dto) { public ResponseEntity<PasteDto.Response> getByKey(
@PathVariable("paste_key") String pasteKey,
@RequestParam(value = "password", required = false) String password
) {
return ResponseEntity.ok( return ResponseEntity.ok(
PasteMapper.toResponse(pasteService.getByKey(pasteKey, dto.password())) PasteMapper.toResponse(pasteService.getByKey(pasteKey, password))
); );
} }
@PostMapping @PostMapping
public ResponseEntity<PasteDto.Response> create(PasteDto.Request request) { public ResponseEntity<PasteDto.Response> create(@RequestBody PasteDto.Request request) {
return ResponseEntity.ok( return ResponseEntity.ok(
PasteMapper.toResponse( PasteMapper.toResponse(
pasteService.create( pasteService.create(

View File

@@ -22,7 +22,7 @@ public class Paste {
@Transient @Transient
private UUID ownerId; private UUID ownerId;
@Column(name = "paste_key", updatable = false, insertable = false, unique = true, nullable = false) @Column(name = "paste_key", updatable = false, unique = true, nullable = false)
private String pasteKey; private String pasteKey;
@Column(nullable = false) @Column(nullable = false)

View File

@@ -3,6 +3,7 @@ package net.miarma.backend.mpaste.service;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import net.miarma.backend.mpaste.model.Paste; import net.miarma.backend.mpaste.model.Paste;
import net.miarma.backend.mpaste.repository.PasteRepository; import net.miarma.backend.mpaste.repository.PasteRepository;
import net.miarma.backend.mpaste.util.PasteKeyGenerator;
import net.miarma.backend.mpaste.validation.PasteValidator; import net.miarma.backend.mpaste.validation.PasteValidator;
import net.miarma.backlib.exception.ForbiddenException; import net.miarma.backlib.exception.ForbiddenException;
import net.miarma.backlib.exception.NotFoundException; import net.miarma.backlib.exception.NotFoundException;
@@ -62,6 +63,8 @@ public class PasteService {
public Paste create(Paste paste) { public Paste create(Paste paste) {
PasteValidator.validate(paste); PasteValidator.validate(paste);
paste.setPasteId(UUID.randomUUID());
paste.setPasteKey(PasteKeyGenerator.generate(6));
return pasteRepository.save(paste); return pasteRepository.save(paste);
} }