Skip to content

Commit

Permalink
Data Normalization Mapper: Enhancements to getAllItems Method
Browse files Browse the repository at this point in the history
Mapper to normalize the information fetched from GeneralItem
and QuantizableItem, restructuring of the 'getAllItems' method
to return the normalized and mapped list, and adding a header
with the attributes returned in the mapper.
  • Loading branch information
superpollo2 committed Apr 19, 2024
1 parent 5a11c01 commit ced21b3
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 18 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ public OpenAPI swagger() {
.bearerFormat("JWT");

// Define security requirement for endpoints that require Bearer token
SecurityRequirement securityRequirement = new SecurityRequirement()
.addList("bearerAuth");
// SecurityRequirement securityRequirement = new SecurityRequirement()
// .addList("bearerAuth");

// Create OpenAPI object and configure servers, info, security schemes, and security requirements
return new OpenAPI()
.info(info)
.components(new io.swagger.v3.oas.models.Components().addSecuritySchemes("bearerAuth", securityScheme))
.addSecurityItem(securityRequirement);
.components(new io.swagger.v3.oas.models.Components().addSecuritySchemes("bearerAuth", securityScheme));
//.addSecurityItem(securityRequirement);


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@Tag(name = "Manage for Inventory", description = "something")
@RestController
Expand Down Expand Up @@ -50,8 +51,9 @@ public void deleteCategory(@PathVariable("itemId") String itemId){
}

@GetMapping
public List<Object> inventoryItems() {
return inventoryItemService.getAllItems();
public Map<String, Object> inventoryItems() {
System.out.println("al menos me dan click");
return inventoryItemService.getAllItemsMapped();
}

@GetMapping(EndpointConstant.ENDPOINT_ONE_GENERAL_ITEM)
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/consola/lis/dto/ItemInfoDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.consola.lis.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ItemInfoDTO {
private String id;
private String state;
private String category;
private String wallet;
private String[] attributes;
private boolean quantizable;
}
42 changes: 37 additions & 5 deletions src/main/java/com/consola/lis/service/InventoryItemService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.consola.lis.service;


import com.consola.lis.dto.ItemInfoDTO;
import com.consola.lis.util.constans.Util;
import com.consola.lis.dto.GeneralItemDTO;
import com.consola.lis.dto.QuantizableItemDTO;
Expand All @@ -15,14 +16,17 @@
import com.consola.lis.model.repository.CategoryRepository;
import com.consola.lis.model.repository.GeneralItemRepository;
import com.consola.lis.model.repository.QuantizableItemRepository;
import com.consola.lis.util.mapper.InventoryItemMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.transaction.Transactional;
import org.json.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.*;


@Service

public class InventoryItemService {
Expand All @@ -32,6 +36,7 @@ public class InventoryItemService {
private final QuantizableItemRepository quantizableItemRepository;
private final CategoryRepository categoryRepository;


private final Map<Boolean, Map<Boolean, List<String>>> validStatesMap = new HashMap<>();

public InventoryItemService(ObjectMapper objectMapper,
Expand Down Expand Up @@ -183,17 +188,44 @@ public boolean existItem(String itemId) {
return generalItemRepository.existsById(itemId);
}

public List<Object> getAllItems() {
public Map<String, Object> getAllItemsMapped() {
List<ItemInfoDTO> inventoryItems = getAllItems();
Map<String, Object> result = new HashMap<>();
result.put("header", createHeader());
result.put("allRegisters", inventoryItems);
return result;
}

private List<String> createHeader() {
List<String> header = new ArrayList<>();
header.add("Id");
header.add("Estado");
header.add("Categoría");
header.add("Wallet");
header.add("Atributos");
return header;
}

private List<ItemInfoDTO> getAllItems() {
List<GeneralItem> generalItems = getAllGeneralItems();
List<Object> inventoryItems = new ArrayList<>(generalItems);
List<QuantizableItem> quantizableItems = getAllQuantizableItems();
inventoryItems.addAll(quantizableItems);

return inventoryItems;
List<ItemInfoDTO> inventoryItems = new ArrayList<>();

}
generalItems.forEach(generalItem -> {
Category category = categoryRepository.findCategoryById(generalItem.getCategoryId());
inventoryItems.add(InventoryItemMapper.mapToItemInfoDTO(generalItem, category));
});

// Mapear los QuantizableItems
quantizableItems.forEach(quantizableItem -> {
GeneralItem generalItem = quantizableItem.getGeneralItem();
Category category = categoryRepository.findCategoryById(generalItem.getCategoryId());
inventoryItems.add(InventoryItemMapper.mapToItemInfoDTO(generalItem, category));
});


return inventoryItems;
}

}
2 changes: 1 addition & 1 deletion src/main/java/com/consola/lis/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.consola.lis.dto.AuthResponseDTO;
import com.consola.lis.dto.UserDTO;
import com.consola.lis.mapper.UserMapper;
import com.consola.lis.util.mapper.UserMapper;
import com.consola.lis.util.exception.NotExistingException;
import com.consola.lis.jwt.JwtService;
import com.consola.lis.model.entity.User;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ public class EndpointConstant {

//Endpoints authentication
public static final String ENDPOINT_LOGIN = "/api/console-lis/auth/login";
public static final String ENDPOINT_REGISTER = "/api/console-lis/admin/register";
public static final String ENDPOINT_REGISTER = "/api/console-lis/auth/register";


//Endpoints Category
public static final String ENDPOINT_CATEGORY = "/api/console-lis/user/category";
public static final String ENDPOINT_CATEGORY = "/api/console-lis/auth/category";
public static final String ENDPOINT_DELETE_CATEGORY = "/{categoryName}";
public static final String ENDPOINT_ONE_CATEGORY = "/{categoryName}";
public static final String ENDPOINT_ALL_NAMES_CATEGORYS = "/categoriesNames";


//Endpoints Inventory Item
public static final String ENDPOINT_INVENTORY = "/api/console-lis/user/inventory";
public static final String ENDPOINT_INVENTORY = "/api/console-lis/auth/inventory";

public static final String ENDPOINT_INVENTORY_GENERAL_ITEM = "/generalItem";
public static final String ENDPOINT_INVENTORY_QUANTIZABLE_ITEM = "/quantizableItem";
Expand All @@ -33,7 +33,7 @@ public class EndpointConstant {

//user

public static final String ENDPOINT_USER = "/api/console-lis/user";
public static final String ENDPOINT_USER = "/api/console-lis/auth/user";
public static final String ENDPOINT_ONE_USER ="/{username}";
public static final String ENDPOINT_USER_LDAP = "/ldapUser/{username}";
public static final String ENDPOINT_DELETE_USER = "/delete/{username}";
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/consola/lis/util/mapper/InventoryItemMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.consola.lis.util.mapper;

import com.consola.lis.dto.CategoryDTO;
import com.consola.lis.dto.ItemInfoDTO;
import com.consola.lis.dto.UserDTO;
import com.consola.lis.model.entity.Category;
import com.consola.lis.model.entity.GeneralItem;
import com.consola.lis.model.entity.User;
import org.json.JSONObject;

import java.util.Set;

public class InventoryItemMapper {
private InventoryItemMapper () {
}
public static ItemInfoDTO mapToItemInfoDTO(GeneralItem generalItem, Category category) {

return ItemInfoDTO.builder()
.id(generalItem.getItemId())
.state(String.valueOf(generalItem.getState()))
.category(category.getCategoryName())
.wallet(String.valueOf(generalItem.getWallet()).replace("_"," "))
.quantizable(category.getQuantizable())
.attributes(convertAttributes(generalItem.getAttributes()))
.build();
}


private static String[] convertAttributes(String attributes){
JSONObject jsonObject = new JSONObject(attributes);
Set<String> keySet = jsonObject.keySet();
return keySet.toArray(new String[0]);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.consola.lis.mapper;
package com.consola.lis.util.mapper;

import com.consola.lis.dto.UserDTO;
import com.consola.lis.model.entity.User;
Expand Down
1 change: 0 additions & 1 deletion src/test/java/com/consola/lis/service/UserServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.consola.lis.dto.AuthResponseDTO;
import com.consola.lis.dto.UserDTO;
import com.consola.lis.mapper.UserMapper;
import com.consola.lis.util.exception.NotExistingException;
import com.consola.lis.jwt.JwtService;
import com.consola.lis.model.entity.User;
Expand Down

0 comments on commit ced21b3

Please sign in to comment.