added custom id generator
This commit is contained in:
@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.*;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/api/paste")
|
||||
public class PasteController {
|
||||
|
||||
private final PasteService service;
|
||||
@@ -17,17 +18,17 @@ public class PasteController {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
@GetMapping("/api/paste/findAll")
|
||||
@GetMapping(value = "/findAll")
|
||||
public ResponseEntity<List<Paste>> findAll() {
|
||||
return ResponseEntity.ok(service.findAll());
|
||||
}
|
||||
|
||||
@PostMapping("/api/paste/create")
|
||||
@PostMapping(value = "/create")
|
||||
public ResponseEntity<Paste> create(@RequestBody PasteCreateRequest pasteCreateRequest) {
|
||||
return ResponseEntity.ok(service.save(pasteCreateRequest));
|
||||
}
|
||||
|
||||
@GetMapping("/api/paste/{id}")
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Paste> findById(@PathVariable String id) {
|
||||
return ResponseEntity.ok(service.findById(id));
|
||||
}
|
||||
|
||||
@@ -4,21 +4,41 @@ 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.apache.commons.lang3.RandomUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collector;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Service
|
||||
public class PasteServiceImpl implements PasteService {
|
||||
|
||||
private final String BASE64_CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
private final int BASE64_LENGTH = 64;
|
||||
|
||||
private final PasteRepository repository;
|
||||
|
||||
public PasteServiceImpl(PasteRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
private String generateId(int length) {
|
||||
String id = Stream.generate(() -> BASE64_CHARSET.charAt(RandomUtils.secure().randomInt(0, BASE64_LENGTH)))
|
||||
.limit(10)
|
||||
.collect(Collector.of(
|
||||
StringBuilder::new,
|
||||
StringBuilder::append,
|
||||
StringBuilder::append,
|
||||
StringBuilder::toString
|
||||
));
|
||||
|
||||
if (repository.existsById(id)) id = generateId(length);
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Paste findById(String id) {
|
||||
return repository.findById(id).orElse(null);
|
||||
@@ -33,7 +53,7 @@ public class PasteServiceImpl implements PasteService {
|
||||
public Paste save(PasteCreateRequest pasteCreateRequest) {
|
||||
|
||||
Paste paste = Paste.builder()
|
||||
.id(RandomStringUtils.secure().nextAlphanumeric(10))
|
||||
.id(generateId(10))
|
||||
.title(pasteCreateRequest.getTitle())
|
||||
.content(pasteCreateRequest.getContent())
|
||||
.isPrivate(pasteCreateRequest.getIsPrivate())
|
||||
|
||||
Reference in New Issue
Block a user