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

Change Item with location working correctly.

parent 1678c28a
No related branches found
No related tags found
No related merge requests found
......@@ -8,6 +8,8 @@ import './OrganizationItemDetails.css';
const OrganizationItemDetails = ({ token }) => {
const { orgId, itemId } = useParams();
const [itemInfo, setItemInfo] = useState(null);
const [locations, setLocations] = useState([]);
const [modifiedLocation, setModifiedLocation] = useState('');
const [modifiedFields, setModifiedFields] = useState({
status: '',
category: '',
......@@ -72,6 +74,8 @@ const OrganizationItemDetails = ({ token }) => {
modifiedFieldsToSend[key] = itemInfo.data[1];
else if (key === 'description')
modifiedFieldsToSend[key] = itemInfo.data[2];
else if (key === 'location')
modifiedFieldsToSend[key] = itemInfo.data[9];
}
});
......@@ -108,6 +112,24 @@ const OrganizationItemDetails = ({ token }) => {
// Fetch item details on component mount
useEffect(() => {
const fetchLocations = async () => {
try {
const response = await Axios.post('http://localhost:8080/item/user/location', {
orgId: parseInt(orgId),
jwt: token.jwt,
});
if (response.data.result === 'success') {
setLocations(response.data.data);
} else {
console.error('Error fetching locations');
}
} catch (error) {
console.error('Error fetching locations:', error);
}
};
fetchLocations();
const fetchItemDetails = async () => {
try {
const response = await Axios.post('http://localhost:8080/item/user/oneitem', {
......@@ -198,6 +220,19 @@ const OrganizationItemDetails = ({ token }) => {
/>
</label>
</div>
<label>
Location:
<select
value={modifiedFields.locationId}
onChange={(e) => setModifiedFields( { ...modifiedFields, locationId:e.target.value})}
>
{locations.map(([locationName, locationId], index) => (
<option key={locationId} value={locationId}>
{locationName}
</option>
))}
</select>
</label>
<button onClick={handleModifyItem} className="modify-item-button">
Modify Item
</button>
......
......@@ -55,6 +55,15 @@ public class CustomItemRepository {
.setParameter("itemId", itemId);
return query.executeUpdate();
}
@Transactional
public List<Object> getLocation(Integer orgId)
{
String nativeQuery = "SELECT L.location, L.location_id FROM LOCATION L WHERE L.organization_id = :orgId";
Query query = entityManager.createNativeQuery(nativeQuery)
.setParameter("orgId", orgId);
return query.getResultList();
}
@Transactional
public Object updateItem(Map<String, Object> json)
{
......@@ -71,20 +80,26 @@ public class CustomItemRepository {
else {
quantity = Integer.parseInt((String)json.get("quantity"));
}
Integer locationId;
if (json.get("locationId") instanceof Integer)
locationId = (Integer) json.get("locationId");
else {
locationId = Integer.parseInt((String)json.get("locationId"));
}
String status = (String) json.get("status");
String description = (String) json.get("description");
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";
String nativeQuery = "UPDATE ITEM SET status = ?1, description = ?2, quantity = ?3, category = ?4, name = ?5, location_id = ?6 WHERE item_id = ?7";
Query query = entityManager.createNativeQuery(nativeQuery)
.setParameter(1, status)
.setParameter(2, description)
.setParameter(3, quantity)
.setParameter(4, category)
.setParameter(5, name)
.setParameter(6, itemId);
.setParameter(6, locationId)
.setParameter(7, itemId);
int updatedRows = query.executeUpdate();
......
......@@ -159,16 +159,14 @@ public class ItemController {
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"))
|| !json.containsKey("status") || !json.containsKey("locationId"))
{
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;
......@@ -191,6 +189,35 @@ public class ItemController {
return response;
}
@PostMapping(path="/user/location")
public @ResponseBody Map<String, Object> getLocation(@RequestBody Map<String, Object> json)
{
Map<String, Object> response = new HashMap<>();
if (!json.containsKey("jwt") || !json.containsKey("orgId"))
{
response.put("result", "failure - bad request");
return response;
}
System.out.println(json.entrySet());
Map<String, Object> map = getUserOrg(json);
System.out.println(map.entrySet());
if (map.get("result").equals("success"))
{
Integer orgId;
if (json.get("orgId") instanceof Integer)
orgId = (Integer) json.get("orgId");
else
orgId = Integer.parseInt((String)(json.get("orgId")));
response.put("data", customItemRepository.getLocation(orgId));
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