Skip to content
Snippets Groups Projects
Commit 9c0b4863 authored by Sarthak Shrivastava's avatar Sarthak Shrivastava
Browse files

Added end point for updating items.

parent 97325ffd
No related branches found
No related tags found
No related merge requests found
......@@ -114,7 +114,7 @@ const OrganizationItems = ({ token }) => {
<div className="items-list">
{filteredItems.length > 0 ? (
filteredItems.map((item, index) => (
<div key={index} className="item-item" onClick={() => handleItemClick(item)}>
<div key={index} className="item-item" onClick={() => handleItemClick(item[0])}>
<div className="item-details" >
<span><strong>Name:</strong> {item[1]}</span>
<span><strong>Description:</strong> {item[2]}</span>
......
......@@ -7,6 +7,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
@Repository
public class CustomItemRepository {
......@@ -30,12 +31,20 @@ public class CustomItemRepository {
@Transactional
public Object getItem(Integer orgId, Integer itemId)
{
String nativeQuery = "SELECT I.*, L.location FROM ITEM I JOIN LOCATION L ON I.location_id = L.location_id WHERE I.organization_id = :orgId AND I.item_id = :itemId";
Query query = entityManager.createNativeQuery(nativeQuery)
.setParameter("orgId", orgId)
.setParameter("itemId", itemId);
try
{
String nativeQuery = "SELECT I.*, L.location FROM ITEM I JOIN LOCATION L ON I.location_id = L.location_id WHERE I.organization_id = :orgId AND I.item_id = :itemId";
Query query = entityManager.createNativeQuery(nativeQuery)
.setParameter("orgId", orgId)
.setParameter("itemId", itemId);
return query.getSingleResult();
}
catch (Exception e)
{
return "Item Not Found";
}
return query.getSingleResult();
}
@Transactional
......@@ -46,5 +55,29 @@ public class CustomItemRepository {
.setParameter("itemId", itemId);
return query.executeUpdate();
}
@Transactional
public Object updateItem(Map<String, Object> json)
{
// Extract parameters from the JSON map
Integer itemId = (Integer) json.get("itemId");
String status = (String) json.get("status");
String description = (String) json.get("description");
Integer quantity = (Integer) json.get("quantity");
String category = (String) json.get("category");
String name = (String) json.get("name");
// Use native SQL query with EntityManager to update the item
String nativeQuery = "UPDATE ITEM SET status = ?1, description = ?2, quantity = ?3, category = ?4, name = ?5 WHERE item_id = ?6";
Query query = entityManager.createNativeQuery(nativeQuery)
.setParameter(1, status)
.setParameter(2, description)
.setParameter(3, quantity)
.setParameter(4, category)
.setParameter(5, name)
.setParameter(6, itemId);
int updatedRows = query.executeUpdate();
return updatedRows > 0;
}
}
......@@ -155,7 +155,41 @@ public class ItemController {
}
return response;
}
@PutMapping(path="/user/oneitem")
public @ResponseBody Map<String, Object> updateItemToken(@RequestBody Map<String, Object> json)
{
Map<String, Object> response = new HashMap<>();
System.out.println(json.entrySet());
if (!json.containsKey("orgId") || !json.containsKey("jwt") || !json.containsKey("itemId") || !json.containsKey("name")
|| !json.containsKey("description") || !json.containsKey("quantity") || !json.containsKey("category")
|| !json.containsKey("status"))
{
response.put("result", "failure - bad request");
return response;
}
Map<String, Object> map = getUserOrg(json);
System.out.println(map.entrySet());
if (map.get("result").equals("success"))
{
Integer itemId;
if (map.get("type") != OrganizationRoster.Type.MANAGER && map.get("type") != OrganizationRoster.Type.OWNER)
{
response.put("result", "failure - not authorized, user cannot delete");
return response;
}
if (json.get("itemId") instanceof Integer)
itemId = (Integer) json.get("itemId");
else
itemId = Integer.parseInt((String)(json.get("itemId")));
response.put("data", customItemRepository.updateItem(json));
response.put("result", "success");
}
else
{
response.put("result", "failure - not authorized");
}
return response;
}
/*
Private helper function to validate that the user is supposed to see this data
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment