Skip to content

Commit

Permalink
Modificado: Tablas "unit_types", "shopping_list" y "products_has_shop…
Browse files Browse the repository at this point in the history
…ping_list"

- Se cambia el tipo de la columna "id" por el tipo "BIGINT".
- Se actualiza las entidades de java y todas las clases donde se utiliza el campo ID.
- Agregado script para flyway.
  • Loading branch information
nmarulo committed Nov 10, 2024
1 parent 287dd07 commit 5f44395
Show file tree
Hide file tree
Showing 24 changed files with 61 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ public class SaveShoppingListProductReq {

private Long productId;

private Integer shoppingListId;
private Long shoppingListId;

private Integer unitsPerProduct;

private Integer unitTypeId;
private Long unitTypeId;

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class SaveShoppingListProductRes {
@NoArgsConstructor
public static class ShoppingList {

private Integer id;
private Long id;

private String name;

Expand All @@ -48,7 +48,7 @@ public static class Product {
@NoArgsConstructor
public static class UnitType {

private Integer id;
private Long id;

private String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ public class ProductHasShoppingListPK implements Serializable {
private Long productId;

@Column(name = "shopping_list_id", nullable = false)
private Integer shoppingListId;
private Long shoppingListId;

@Column(name = "unit_type_id", nullable = false)
private Integer unitTypeId;
private Long unitTypeId;

public String toString() {return "ProductHasShoppingListPK(productId=" + this.getProductId() + ", shoppingListId=" + this.getShoppingListId() + ", unitTypeId=" + this.getUnitTypeId() + ")";}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ Optional<ProductHasShoppingList> findByShoppingListIdAndUserAndProductId(Integer

@Query(
"SELECT p FROM ProductHasShoppingList p WHERE p.shoppingList.id = :id AND p.shoppingList.user = :user AND p.product.id IN :productsId")
List<ProductHasShoppingList> findAllByShoppingListIdAndUserAndProductIdIn(Integer id,
List<ProductHasShoppingList> findAllByShoppingListIdAndUserAndProductIdIn(Long id,
User user,
List<Long> productsId);

@Query(
"SELECT p FROM ProductHasShoppingList p WHERE p.shoppingList.id = :id AND p.shoppingList.user = :user AND p.product.id IN :productsId AND p.unitType.id IN :unitTypesId")
List<ProductHasShoppingList> findAllByShoppingListIdAndUserAndProductIdInAndUnitTypeIdIn(Integer id,
List<ProductHasShoppingList> findAllByShoppingListIdAndUserAndProductIdInAndUnitTypeIdIn(Long id,
User user,
List<Long> productsId,
List<Integer> unitTypesId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class ShoppingList {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "id", nullable = false)
private Integer id;
private Long id;

@Basic
@Column(name = "name", length = 250)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@RequiredArgsConstructor
@Getter
@Tag(name = "Shopping List Admin", description = "Endpoints for managing shopping lists")
public class ShoppingListAdminController extends CrudController<ShoppingListAdminReq, ShoppingListAdminRes, Integer> {
public class ShoppingListAdminController extends CrudController<ShoppingListAdminReq, ShoppingListAdminRes, Long> {

private final ShoppingListAdminService service;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Service
@RequiredArgsConstructor
@Getter
public class ShoppingListAdminService extends CrudServiceImp<ShoppingListAdminReq, ShoppingListAdminRes, ShoppingList, Integer> {
public class ShoppingListAdminService extends CrudServiceImp<ShoppingListAdminReq, ShoppingListAdminRes, ShoppingList, Long> {

private final ShoppingListRepository repository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public ResponseEntity<FindAllShoppingListRes> findAll(@PageableDefault Pageable
}

@GetMapping("/{id}")
public ResponseEntity<FindByIdShoppingListRes> findById(@PathVariable Integer id) {
public ResponseEntity<FindByIdShoppingListRes> findById(@PathVariable Long id) {
return ResponseEntity.ok(this.shoppingListService.findById(id,
this.dataRequestScope.getAuthenticationPrincipal()));
}
Expand All @@ -40,8 +40,7 @@ public ResponseEntity<FindByIdProductShoppingListRest> findByIdProduct(@PathVari
}

@DeleteMapping("/{id}/products")
public ResponseEntity<?> deleteProducts(@PathVariable Integer id,
@RequestBody DeleteProductsShoppingListReq request) {
public ResponseEntity<?> deleteProducts(@PathVariable Long id, @RequestBody DeleteProductsShoppingListReq request) {
this.shoppingListService.deleteProducts(id, request, this.dataRequestScope.getAuthenticationPrincipal());

return ResponseEntity.noContent()
Expand All @@ -55,15 +54,15 @@ public ResponseEntity<SaveShoppingListRes> save(@RequestBody SaveShoppingListReq
}

@PutMapping("/{id}")
public ResponseEntity<UpdateShoppingListRes> update(@PathVariable Integer id,
public ResponseEntity<UpdateShoppingListRes> update(@PathVariable Long id,
@RequestBody UpdateShoppingListReq request) {
return ResponseEntity.ok(this.shoppingListService.update(id,
request,
this.dataRequestScope.getAuthenticationPrincipal()));
}

@DeleteMapping("/{id}")
public ResponseEntity<?> delete(@PathVariable Integer id) {
public ResponseEntity<?> delete(@PathVariable Long id) {
this.shoppingListService.delete(id, this.dataRequestScope.getAuthenticationPrincipal());

return ResponseEntity.noContent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import java.util.Optional;

@Repository
public interface ShoppingListRepository extends JpaRepository<ShoppingList, Integer> {
public interface ShoppingListRepository extends JpaRepository<ShoppingList, Long> {

Page<ShoppingList> findAllByUser(User user, Pageable pageable);

Optional<ShoppingList> findByIdAndUser(Integer id, User user);
Optional<ShoppingList> findByIdAndUser(Long id, User user);

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public FindAllShoppingListRes findAll(final Pageable pageable, User user) {
return ShoppingListMapper.toFindAllShoppingListRes(pageFindAll);
}

public FindByIdShoppingListRes findById(Integer id, User user) {
public FindByIdShoppingListRes findById(Long id, User user) {
var findById = this.shoppingListRepository.findByIdAndUser(id, user);

if (findById.isEmpty()) {
Expand All @@ -57,7 +57,7 @@ public FindByIdProductShoppingListRest findByIdProduct(Integer id, Long productI
return ShoppingListMapper.toFindByIdProductShoppingListRest(productShoppingListOptional.get());
}

public void deleteProducts(Integer id, DeleteProductsShoppingListReq request, User user) {
public void deleteProducts(Long id, DeleteProductsShoppingListReq request, User user) {
var shoppingListOptional = this.shoppingListRepository.findByIdAndUser(id, user);

if (shoppingListOptional.isEmpty()) {
Expand Down Expand Up @@ -97,7 +97,7 @@ public SaveShoppingListRes save(SaveShoppingListReq request, User user) {
return ShoppingListMapper.toSaveShoppingListRes(save);
}

public UpdateShoppingListRes update(Integer id, UpdateShoppingListReq request, User user) {
public UpdateShoppingListRes update(Long id, UpdateShoppingListReq request, User user) {
var shoppingListOptional = this.shoppingListRepository.findByIdAndUser(id, user);

if (shoppingListOptional.isEmpty()) {
Expand All @@ -115,7 +115,7 @@ public UpdateShoppingListRes update(Integer id, UpdateShoppingListReq request, U
return ShoppingListMapper.toUpdateShoppingListRes(update);
}

public void delete(Integer id, User user) {
public void delete(Long id, User user) {
var shoppingListOptional = this.shoppingListRepository.findByIdAndUser(id, user);

if (shoppingListOptional.isEmpty()) {
Expand All @@ -128,7 +128,7 @@ public void delete(Integer id, User user) {
this.shoppingListRepository.delete(shoppingList);
}

private void updateProducts(Integer shoppingListId,
private void updateProducts(Long shoppingListId,
List<UpdateShoppingListReq.ProductShoppingList> productsReq,
User user) {
if (productsReq == null || productsReq.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class FindAllShoppingListRes extends PagingAndSortingRes<FindAllShoppingL
@Data
public static class ShoppingList {

private Integer id;
private Long id;

private String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class FindByIdProductShoppingListRest {
@NoArgsConstructor
public static class UnitTypeRes {

private Integer id;
private Long id;

private String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@Data
public class FindByIdShoppingListRes {

private Integer id;
private Long id;

private String name;

Expand Down Expand Up @@ -47,7 +47,7 @@ public static class Product {
@Data
public static class UnitType {

private Integer id;
private Long id;

private String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@AllArgsConstructor
public class SaveShoppingListRes {

private Integer id;
private Long id;

private String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
@AllArgsConstructor
public class UpdateShoppingListRes {

private Integer id;
private Long id;

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class UnitType {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "id", nullable = false)
private Integer id;
private Long id;

@Basic
@Column(name = "name", nullable = false, length = 50)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@RequiredArgsConstructor
@Getter
@Tag(name = "Unit Type Admin", description = "Endpoints for managing unit types")
public class UnitTypeAdminController extends CrudController<UnitTypeAdminReq, UnitTypeAdminRes, Integer> {
public class UnitTypeAdminController extends CrudController<UnitTypeAdminReq, UnitTypeAdminRes, Long> {

private final UnitTypeAdminService service;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Service
@RequiredArgsConstructor
@Getter
public class UnitTypeAdminService extends CrudServiceImp<UnitTypeAdminReq, UnitTypeAdminRes, UnitType, Integer> {
public class UnitTypeAdminService extends CrudServiceImp<UnitTypeAdminReq, UnitTypeAdminRes, UnitType, Long> {

private final UnitTypeRepository repository;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
import org.springframework.stereotype.Repository;

@Repository
public interface UnitTypeRepository extends JpaRepository<UnitType, Integer> {}
public interface UnitTypeRepository extends JpaRepository<UnitType, Long> {}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class FindAllUnitTypeRes extends PagingAndSortingRes<FindAllUnitTypeRes.U
@Data
public static class UnitType {

public Integer id;
public Long id;

public String name;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class FindByIdUserRes {
@Data
public static class ShoppingList {

private Integer id;
private Long id;

private String name;

Expand Down
25 changes: 25 additions & 0 deletions src/main/resources/db/migration/mysql/V4__update_column_type.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
ALTER TABLE products_has_shopping_list
DROP FOREIGN KEY fk_products_has_shopping_list_shopping_list;
ALTER TABLE products_has_shopping_list
MODIFY shopping_list_id BIGINT;
ALTER TABLE shopping_list
MODIFY id BIGINT AUTO_INCREMENT;
ALTER TABLE products_has_shopping_list
ADD CONSTRAINT fk_products_has_shopping_list_shopping_list
FOREIGN KEY (shopping_list_id)
REFERENCES shopping_list (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

ALTER TABLE products_has_shopping_list
DROP FOREIGN KEY fk_products_has_shopping_list_unit_types;
ALTER TABLE products_has_shopping_list
MODIFY unit_type_id BIGINT;
ALTER TABLE unit_types
MODIFY id BIGINT AUTO_INCREMENT;
ALTER TABLE products_has_shopping_list
ADD CONSTRAINT fk_products_has_shopping_list_unit_types
FOREIGN KEY (unit_type_id)
REFERENCES unit_types (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private User initUser() {
private ShoppingList initShoppingList(User user) {
final var shoppingList = new ShoppingList();

shoppingList.setId(FakeTestUtil.randomInteger());
shoppingList.setId(FakeTestUtil.randomLong());
shoppingList.setName(FakeTestUtil.randomSentence());
shoppingList.setTotalProducts(0);
shoppingList.setTotalCalories(FakeTestUtil.randomBigDecimal());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import java.time.LocalDateTime;
import java.util.Collections;

@Getter
Expand Down Expand Up @@ -44,7 +43,7 @@ private FindAllUnitTypeRes initFinalAllUnitTypeRes(Page<UnitType> unitTypePage)
private UnitType initUnitType() {
final var unitType = new UnitType();

unitType.setId(FakeTestUtil.randomInteger());
unitType.setId(FakeTestUtil.randomLong());
unitType.setName(FakeTestUtil.randomWord());
unitType.setCreatedAt(FakeTestUtil.randomPast());
unitType.setUpdatedAt(FakeTestUtil.randomFuture());
Expand Down

0 comments on commit 5f44395

Please sign in to comment.