Skip to content
Snippets Groups Projects
Commit af44b0be authored by harryTheWizzard's avatar harryTheWizzard
Browse files

changed all from model to jpa entity

parent 18dbcf41
No related branches found
No related tags found
No related merge requests found
Showing
with 106 additions and 37 deletions
package ntnu.idatt2016.v233.SmartMat.entity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
/**
* This is a record class representing an allergy
*
* @author Stian Lyng
* @version 1.0
* @version 1.1
*
* @param name The name of the allergy
* @param description The description of the allergy
*/
public record Allergy(String name, String description) {
@Entity
public class Allergy{
@Id
@Column(name = "name")
String name;
@Column(name = "description")
String description;
}
\ No newline at end of file
package ntnu.idatt2016.v233.SmartMat.entity;
import jakarta.persistence.*;
/**
* Fridge is a record class representing a fridge in the system.
*
* @author Anders
* @version 1.0
* @since 04.04.2023
* @version 1.1
* @since 05.04.2023
*
* @param fridgeId
* @param groupId
*/
public record Fridge(long fridgeId, long groupId) {
@Entity
public class Fridge{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "fridgeId")
long fridgeId;
@Column(name = "groupId")
long groupId;
}
package ntnu.idatt2016.v233.SmartMat.entity;
import jakarta.persistence.*;
/**
* Recipe is a record class representing a recipe in the system.
*
......@@ -11,5 +13,15 @@ package ntnu.idatt2016.v233.SmartMat.entity;
* @param name
* @param description
*/
public record Recipe(long id, String name, String description) {
@Entity
public class Recipe {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
long id;
@Column(name = "name")
String name;
@Column(name = "description")
String description;
}
package ntnu.idatt2016.v233.SmartMat.entity;
import jakarta.persistence.*;
/**
* This class represents a shopping list
*
......@@ -10,5 +12,12 @@ package ntnu.idatt2016.v233.SmartMat.entity;
* @param ShoppingListID the id of the shopping list
* @param GroupID the id of the groupID
*/
public record ShoppingList(long ShoppingListID, long groupID) {
@Entity
public class ShoppingList {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
long ShoppingListID;
@Column(name = "groupID")
long groupID;
}
package ntnu.idatt2016.v233.SmartMat.entity.product;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
/**
* Product is a record class representing a product in the system.
* All other info about the product is fetched from api call on fronted.
* this ensures that the product is always up to date.
* @author Birk
* @version 1.0
* @since 04.04.2023
* @version 1.1
* @since 05.04.2023
*/
@Builder
public record Product (int ean, String name, String description){
@Entity(name = "product")
@Data
public class Product{
@Id
@Column(name = "ean")
long ean;
@Column(name = "item_name")
String name;
@Column(name = "description")
String description;
}
......@@ -19,7 +19,7 @@ public interface ProductRepository extends JpaRepository<Product, Long> {
/**
* Gets a Product by name
*
* @param id the ID of the group
* @param name the name of the group
* @return an optional containing the product if it exists
*/
Optional<Product> getByProductName(String name);}
Optional<Product> getByName(String name);}
......@@ -3,6 +3,8 @@ package ntnu.idatt2016.v233.SmartMat.repository;
import java.util.Optional;
import ntnu.idatt2016.v233.SmartMat.entity.Recipe;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* This interface defines the methods for the recipe repository
......
......@@ -4,12 +4,14 @@ import java.util.List;
import java.util.Optional;
import ntnu.idatt2016.v233.SmartMat.entity.ShoppingList;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
/**
* This interface defines the methods for the shopping list repository
*
* @author Stian Lyng
* @version 1.0
* @version 1.1
*
*/
@Repository
......@@ -21,7 +23,7 @@ public interface ShoppingListRepository extends JpaRepository<ShoppingList, Long
* @param id the ID of the group
* @return an optional containing the shopping list if it exists
*/
Optional<ShoppingList> getByGroupID(int id);
Optional<ShoppingList> getByGroupID(long id);
/**
* Gets all shopping lists by group ID
......@@ -29,6 +31,6 @@ public interface ShoppingListRepository extends JpaRepository<ShoppingList, Long
* @param id the ID of the group
* @return an optional containing a list of all shopping lists in the group
*/
Optional<List<ShoppingList>> getAllByGroupID(int id);
List<ShoppingList> getAllByGroupID(long id);
}
......@@ -11,8 +11,8 @@ import java.util.Optional;
* Service for Products
* uses both the ProductRepository and the ProductUtil
* @author Birk
* @version 1.0
* @since 04.04.2023
* @version 1.1
* @since 05.04.2023
*/
public class ProductService {
ProductRepository productRepository;
......@@ -31,16 +31,16 @@ public class ProductService {
* @param id The ID of the product to get
* @return The product with the given ID, if it exists
*/
public Optional<Product> getProductById(int id) {
return productRepository.getById(id);
public Optional<Product> getProductById(Long id) {
return productRepository.findById(id);
}
/**
* Gets all products in the database
* @return All products in the database
*/
public Optional<List<Product>> getAllProducts() {
return productRepository.getAll();
public List<Product> getAllProducts() {
return productRepository.findAll();
}
/**
......@@ -56,7 +56,7 @@ public class ProductService {
* Deletes a product by its ID
* @param id The ID of the product to delete
*/
public void deleteProductById(int id) {
public void deleteProductById(long id) {
productRepository.deleteById(id);
}
......@@ -65,7 +65,7 @@ public class ProductService {
* @return The product with the given name, if it exists
*/
public Optional<Product> getProductByName(String name) {
return productRepository.getByProductName(name);
return productRepository.getByName(name);
}
/**
......@@ -73,11 +73,11 @@ public class ProductService {
* @param id The id of the product to get the volume from
* @return The volume of the product, if it exists
*/
public Optional<String> getProductVolume(int id) {
if(productRepository.getById(id).isEmpty())
public Optional<String> getProductVolume(long id) {
if(productRepository.findById(id).isEmpty())
return Optional.empty();
return ProductUtil.getVolumeFromProduct(productRepository.getById(id).get());
return ProductUtil.getVolumeFromProduct(productRepository.findById(id).get());
}
......
......@@ -10,7 +10,7 @@ import ntnu.idatt2016.v233.SmartMat.repository.ShoppingListRepository;
* Service for the shopping list
*
* @author Stian Lyng
* @version 1.0
* @version 1.1
*/
public class ShoppingListService {
......@@ -39,8 +39,8 @@ public class ShoppingListService {
* @param id the ID of the shopping list
* @return an optional containing the shopping list if it exists
*/
public Optional<ShoppingList> getShoppingListById(int id) {
return shoppingListRepository.getById(id);
public Optional<ShoppingList> getShoppingListById(long id) {
return shoppingListRepository.findById(id);
}
/**
......@@ -58,8 +58,8 @@ public class ShoppingListService {
*
* @return an optional containing a list of all shopping lists if they exist
*/
public Optional<List<ShoppingList>> getAllShoppingLists() {
return shoppingListRepository.getAll();
public List<ShoppingList> getAllShoppingLists() {
return shoppingListRepository.findAll();
}
/**
......@@ -68,7 +68,7 @@ public class ShoppingListService {
* @param id the ID of the group
* @return an optional containing a list of all shopping lists if they exist
*/
public Optional<List<ShoppingList>> getAllShoppingListsByGroupId(int id) {
public List<ShoppingList> getAllShoppingListsByGroupId(long id) {
return shoppingListRepository.getAllByGroupID(id);
}
......@@ -77,7 +77,7 @@ public class ShoppingListService {
*
* @param id the ID of the shopping list
*/
public void deleteShoppingListById(int id) {
public void deleteShoppingListById(long id) {
shoppingListRepository.deleteById(id);
}
......
......@@ -27,7 +27,7 @@ public class ProductUtil {
* @return The volume of the product, if it exists
*/
public static Optional<String> getVolumeFromProduct(Product product) {
for (String desc : Arrays.asList(product.name(), product.description())) {
for (String desc : Arrays.asList(product.getName(), product.getDescription())) {
List<String> words = List.of(desc.split(" "));
if (words.size() > 1) {
String volume = "";
......
# Database connection settings
spring.datasource.url=jdbc:postgres://smartmat_user:Eyhs1OJxyZC56NQCrV7yAolEk9AkLAsC@dpg-cgv5710dh87i4q0fd1b0-a.frankfurt-postgres.render.com/smartmat
spring.datasource.url=jdbc:postgresql://frankfurt-postgres.render.com:5432/smartmat
spring.datasource.username=smartmat_user
spring.datasource.password=Eyhs1OJxyZC56NQCrV7yAolEk9AkLAsC
spring.datasource.driver-class-name=org.postgresql.Driver
# jpa settings
spring.jpa.show-sql=true
## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
## info
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment