refactor: Use UUIDs on Galleries

Remove Long-based IDs and use universally unique IDs.

Closed FS-27
This commit is contained in:
Salmonllama 2023-12-03 07:17:55 -05:00
parent ce593575ee
commit 75a2cdf11e
3 changed files with 11 additions and 7 deletions

View File

@ -11,6 +11,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID;
@RestController @RestController
@RequestMapping("/api/v1/gallery") @RequestMapping("/api/v1/gallery")
@ -26,7 +27,7 @@ public class GalleryController {
} }
@GetMapping("/{id}") @GetMapping("/{id}")
public ResponseEntity<Gallery> getGalleryById(@PathVariable(value = "id") Long galleryId) throws ResourceNotFoundException { public ResponseEntity<Gallery> getGalleryById(@PathVariable(value = "id") UUID galleryId) throws ResourceNotFoundException {
Gallery gallery = galleryRepository.findById(galleryId).orElseThrow(() -> new ResourceNotFoundException("Gallery not found for id :: " + galleryId)); Gallery gallery = galleryRepository.findById(galleryId).orElseThrow(() -> new ResourceNotFoundException("Gallery not found for id :: " + galleryId));
return ResponseEntity.ok().body(gallery); return ResponseEntity.ok().body(gallery);
@ -54,7 +55,7 @@ public class GalleryController {
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public ResponseEntity<Gallery> updateGallery(@PathVariable(value = "id") Long galleryId, @Valid @RequestBody Gallery galleryDetails) throws ResourceNotFoundException { public ResponseEntity<Gallery> updateGallery(@PathVariable(value = "id") UUID galleryId, @Valid @RequestBody Gallery galleryDetails) throws ResourceNotFoundException {
Gallery gallery = galleryRepository.findById(galleryId).orElseThrow(() -> new ResourceNotFoundException("Gallery not found for id :: " + galleryId)); Gallery gallery = galleryRepository.findById(galleryId).orElseThrow(() -> new ResourceNotFoundException("Gallery not found for id :: " + galleryId));
gallery gallery
@ -70,7 +71,7 @@ public class GalleryController {
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")
public ResponseEntity<Map<String, Boolean>> deleteGallery(@PathVariable(value = "id") Long galleryId) throws ResourceNotFoundException { public ResponseEntity<Map<String, Boolean>> deleteGallery(@PathVariable(value = "id") UUID galleryId) throws ResourceNotFoundException {
Gallery gallery = galleryRepository.findById(galleryId).orElseThrow(() -> new ResourceNotFoundException("Gallery not found for id :: " + galleryId)); Gallery gallery = galleryRepository.findById(galleryId).orElseThrow(() -> new ResourceNotFoundException("Gallery not found for id :: " + galleryId));
galleryRepository.delete(gallery); galleryRepository.delete(gallery);

View File

@ -2,10 +2,12 @@ package io.salmonllama.fashionscapeapi.model;
import jakarta.persistence.*; import jakarta.persistence.*;
import java.util.UUID;
@Entity @Entity
@Table(name = "gallery") @Table(name = "gallery")
public class Gallery { public class Gallery {
private Long id; private UUID id;
private String serverId; private String serverId;
private String channelId; private String channelId;
private String tag; private String tag;
@ -18,10 +20,10 @@ public class Gallery {
@Id @Id
@GeneratedValue @GeneratedValue
@Column(name = "id") @Column(name = "id")
public Long getId() { public UUID getId() {
return id; return id;
} }
public Gallery setId(Long id) { public Gallery setId(UUID id) {
this.id = id; this.id = id;
return this; return this;
} }

View File

@ -6,8 +6,9 @@ import org.springframework.data.jpa.repository.Query;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
public interface GalleryRepository extends JpaRepository<Gallery, Long> { public interface GalleryRepository extends JpaRepository<Gallery, UUID> {
@Query(value = "SELECT g FROM gallery g WHERE g.server_id = ?1", nativeQuery = true) @Query(value = "SELECT g FROM gallery g WHERE g.server_id = ?1", nativeQuery = true)
List<Gallery> findAllByServer(String serverId); List<Gallery> findAllByServer(String serverId);