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

Changes to SQL file to support Item categories, Item class. Incomplete controller code.

parent 8ded46bb
No related branches found
No related tags found
No related merge requests found
......@@ -56,7 +56,7 @@ VALUES
('johnsmith@example.com', 2, 'MEMBER'),
('johnsmith@example.com', 4, 'MANAGER'),
('alicedoe@example.com', 5, 'MEMBER'),
('emilyjohnson@example.com', 2, 'MEMBER');
('emilyjohnson@example.com', 2, 'MANAGER');
# INSERT INTO ORGANIZATION_ROSTER(user_email, organization_id, type)
# VALUES
......@@ -100,21 +100,42 @@ CREATE TABLE IF NOT EXISTS LOCATION (
CONSTRAINT fk_organization_location FOREIGN KEY (organization_id) REFERENCES ORGANIZATION (organization_id) ON DELETE CASCADE
);
INSERT INTO LOCATION (location, organization_id)
VALUES
('Student Storage Unit 343', 2),
('Club Room Rack A', 2),
('Club Room Rack B', 2),
('Borrowed', 2);
CREATE TABLE IF NOT EXISTS ITEM (
item_id INT AUTO_INCREMENT,
name VARCHAR(128) NOT NULL,
description VARCHAR(256),
owner_email VARCHAR(128),
quantity INT NOT NULL,
category VARCHAR(128),
category ENUM('STATIONERY', 'MARKETING', 'ELECTRONICS', 'SUPPLIES', 'PERISHABLES', 'MERCHANDISE', 'TOOLS', 'CHEMICALS', 'FLAMMABLE', 'OTHER', 'UNIQUE', 'BOOKS'),
status ENUM('AVAILABLE', 'BORROWED', 'LISTED', 'SOLD') NOT NULL,
location_id INT NOT NULL,
organization_id INT NOT NULL,
PRIMARY KEY (item_id),
CONSTRAINT fk_location_item FOREIGN KEY (location_id) REFERENCES LOCATION (location_id),
CONSTRAINT fk_organiztion_item FOREIGN KEY (organization_id) REFERENCES ORGANIZATION (organization_id) ON DELETE CASCADE
CONSTRAINT fk_organization_item FOREIGN KEY (organization_id) REFERENCES ORGANIZATION (organization_id) ON DELETE CASCADE
);
INSERT INTO ITEM (name, description, owner_email, quantity, category, status, location_id, organization_id)
VALUES
('Chess Set 1', 'Standard chess set with board', 'chesssociety@example.com', 5, 'TOOLS', 'AVAILABLE', 1, 2),
('Chess Clock', 'Digital chess clock for tournaments', 'chesssociety@example.com', 2, 'ELECTRONICS', 'AVAILABLE', 2, 2),
('Chess Strategy Book', 'Guide to advanced chess strategies', 'chesssociety@example.com', 3, 'BOOKS', 'AVAILABLE', 3, 2),
('Chess Tactics Guide', 'Book on improving chess tactics', 'chesssociety@example.com', 4, 'BOOKS', 'AVAILABLE', 1, 2),
('Chess Magazine', 'Latest issue of chess magazine', 'chesssociety@example.com', 1, 'BOOKS', 'AVAILABLE', 2, 2),
('Chess Puzzle Set', 'Collection of challenging chess puzzles', 'chesssociety@example.com', 6, 'TOOLS', 'AVAILABLE', 3, 2),
('Chess Club T-Shirt', 'Official Chess Society T-Shirt', 'chesssociety@example.com', 10, 'MERCHANDISE', 'AVAILABLE', 1, 2),
('Chess Trophy', 'Tournament winner trophy', 'chesssociety@example.com', 1, 'UNIQUE', 'AVAILABLE', 2, 2),
('Chess Analysis Board', 'Board for analyzing game positions', 'chesssociety@example.com', 2, 'TOOLS', 'AVAILABLE', 3, 2),
('Chess Membership Card', 'Official membership card for Chess Society', 'chesssociety@example.com', 1, 'OTHER', 'AVAILABLE', 1, 2);
CREATE TABLE IF NOT EXISTS LISTING (
listing_id INT AUTO_INCREMENT,
item_id INT NOT NULL,
......
package com.example.accessingdatamysql.item;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class CustomItemRepository {
@Autowired
private EntityManager entityManager;
@Transactional
public List<Object[]> getAllItems(Integer orgId)
{
String nativeQuery = "SELECT I.*, L.location FROM ITEM I JOIN LOCATION L ON I.location_id = L.location_id WHERE I.organization_id = ?1";
Query query = entityManager.createNativeQuery(nativeQuery)
.setParameter(1, orgId);
@SuppressWarnings("unchecked")
List<Object[]> resultList = query.getResultList();
return resultList;
}
}
package com.example.accessingdatamysql.item;
import jakarta.persistence.*;
@Entity
@Table(name = "ITEM")
public class Item {
private String name;
private String description;
private String owner_email;
private Integer quantity;
public enum Category
{
STATIONERY,
MARKETING,
ELECTRONICS,
SUPPLIES,
PERISHABLES,
MERCHANDISE,
TOOLS,
CHEMICALS,
FLAMMABLE,
OTHER,
UNIQUE,
BOOKS,
}
@Enumerated(EnumType.STRING)
private Category category;
public enum Status
{
AVAILABLE,
BORROWED
}
@Enumerated(EnumType.STRING)
private Status status;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer locationId;
private Integer organizationId;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getOwner_email() {
return owner_email;
}
public void setOwner_email(String owner_email) {
this.owner_email = owner_email;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public Integer getLocationId() {
return locationId;
}
public void setLocationId(Integer locationId) {
this.locationId = locationId;
}
public Integer getOrganizationId() {
return organizationId;
}
public void setOrganizationId(Integer organizationId) {
this.organizationId = organizationId;
}
}
package com.example.accessingdatamysql.item;
import com.example.accessingdatamysql.myorg.OrgRosterRepository;
import com.example.accessingdatamysql.myorg.OrganizationRoster;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping(path="/item")
public class ItemController {
@Autowired
private OrgRosterRepository orgRosterRepository;
@Autowired
private CustomItemRepository customItemRepository;
@GetMapping(path="/all")
public @ResponseBody Map<String, Object> getItems()
{
Map<String, Object> response = new HashMap<>();
response.put("data", customItemRepository.getAllItems(2));
response.put("result", "success");
return response;
}
}
package com.example.accessingdatamysql.item;
import org.springframework.data.repository.CrudRepository;
public interface ItemRepository extends CrudRepository<Item, Integer> {
}
spring.jpa.hibernate.ddl-auto=update
#spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/inventory
spring.datasource.username=root
spring.datasource.password=APbmCP70!
spring.datasource.password=CSD@mysql-1872
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.show-sql: true
\ No newline at end of file
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