diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Allergy.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Allergy.java index 86a0a1f0264813ce234a8eec011b75b143642fb8..e6cb6fe6a4b0fe364ddc41bda866a034117eaadb 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Allergy.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Allergy.java @@ -1,13 +1,24 @@ 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 diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Fridge.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Fridge.java index f850aa68c8bff4af71dda4594c06409161e19a08..9ed00112ff05b4b8bd497fb6aaf69557935397bd 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Fridge.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Fridge.java @@ -1,14 +1,24 @@ 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; } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Recipe.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Recipe.java index b28757a605179a497b5eef19a89a49b5365405a8..6d861cf3658c9be2278be8fa291464a91361820d 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Recipe.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Recipe.java @@ -1,5 +1,7 @@ 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; } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/ShoppingList.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/ShoppingList.java index 7527fb483d941687e26fb66057d8f53adc7ff95e..75af5c09467f78df4bae7d43b3e8c353c90ea667 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/ShoppingList.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/ShoppingList.java @@ -1,6 +1,8 @@ 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; } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java index 18d145730425a018dda37e64061a2b81c00effc4..670469612cb76826f961a065db448b39765f325d 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Product.java @@ -1,15 +1,29 @@ 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; } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/ProductRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/ProductRepository.java index cb079eb2227357cc362ab5d8d2f85093175e6b8e..93ce2dde396173d448d8603661eaa4ab84cc3ba3 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/ProductRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/ProductRepository.java @@ -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);} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java index 75a8ed80fc026a19ef9f470c797c30d6f909b37a..1283c726b8f9e6021754ce7b50f150fba89b0c86 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/RecipeRepository.java @@ -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 diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/ShoppingListRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/ShoppingListRepository.java index 9a9ed9a7aa584194a7cf38d24efcdced1caa21dc..b4324668ceda105b1f9c599ca36e886f2a013595 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/ShoppingListRepository.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/ShoppingListRepository.java @@ -4,16 +4,18 @@ 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 -public interface ShoppingListRepository extends JpaRepository<ShoppingList, Long>{ +public interface ShoppingListRepository extends JpaRepository<ShoppingList, Long> { /** * Gets a shopping list by its group ID @@ -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); } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ProductService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ProductService.java index 4ef304eb9fc320de95cb79da512a4cb4ea58a243..683441fcbe1721ba8215d555c3b71527b770762a 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ProductService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ProductService.java @@ -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()); } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ShoppingListService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ShoppingListService.java index 6749850d2f33c4bb824baa1df177da89cacd1415..3e3c11982f77b66054dd1c48d5e3162e6c3501f4 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ShoppingListService.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/ShoppingListService.java @@ -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); } diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/ProductUtil.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/ProductUtil.java index 4b70fed0852466f7e5d249d442d69a1e789b1d9c..6aa4a7062447f2d514ee41d811716d7ed8a7056a 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/util/ProductUtil.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/util/ProductUtil.java @@ -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 = ""; diff --git a/target/classes/application.properties b/target/classes/application.properties index dced60aeaf842055576077ca8aa8073107867656..ba7e37ce97efe8800019b8643c642a1f1746bca4 100644 --- a/target/classes/application.properties +++ b/target/classes/application.properties @@ -1,9 +1,18 @@ # 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