diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/FavoriteRequest.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/FavoriteRequest.java new file mode 100644 index 0000000000000000000000000000000000000000..dcb4dd85fac54e0b69761747a1ec246fa590560b --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/dto/request/FavoriteRequest.java @@ -0,0 +1,26 @@ +package ntnu.idatt2016.v233.SmartMat.dto.request; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * This class represents a request for a shopping list. + */ +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class FavoriteRequest { + + /** + * The unique identifier of the shopping list. + */ + private long recipeId; + + /** + * The unique identifier of the group for which the shopping list is requested. + */ + private String username; +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Favourite.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Favourite.java new file mode 100644 index 0000000000000000000000000000000000000000..03f37e787763a595a0d12c23ac0bf70007ea03db --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/Favourite.java @@ -0,0 +1,24 @@ +package ntnu.idatt2016.v233.SmartMat.entity; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Entity(name = "favorite") +@Data +public class Favourite { + + @Id + @Column(name = "recipe_id") + long recipeId; + + @Column(name = "username") + String username; +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Category.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Category.java index cc26e4599ac6eff10539389a4b957e78f9ac4761..ce588654bd5c1e97e8152dce219a564da0700737 100644 --- a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Category.java +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/product/Category.java @@ -15,7 +15,7 @@ import lombok.NoArgsConstructor; @Data public class Category { @Id - @Column(name = "cateogry_name") + @Column(name = "category_name") String ean; @Column(name = "category_description") String description; diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/Achievement.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/Achievement.java new file mode 100644 index 0000000000000000000000000000000000000000..68bea41373e0a183140c5fc5bf9c59b6ea369fe7 --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/entity/user/Achievement.java @@ -0,0 +1,33 @@ +package ntnu.idatt2016.v233.SmartMat.entity.user; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * Achievement is an entity class representing an achievement in the system. + * + * @author Anders + * @version 1.0 + * @since 19.04.2023 + * + */ + +@Data +@NoArgsConstructor +@AllArgsConstructor +@Builder +@Entity(name = "achievement") +public class Achievement { + + @Id + @Column(name = "achievement_name") + private String achievementName; + + @Column(name = "achievement_description") + private String achievementDescription; +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/FavoriteRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/FavoriteRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..7f266bf30ef18e5a7d59b34306dcf14f666c40ae --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/FavoriteRepository.java @@ -0,0 +1,27 @@ +package ntnu.idatt2016.v233.SmartMat.repository; + +import java.util.List; + +import org.springframework.data.jpa.repository.JpaRepository; + +import ntnu.idatt2016.v233.SmartMat.entity.Favourite; + +public interface FavoriteRepository extends JpaRepository<Favourite, Long> { + + /** + * Gets all favorites by username + * + * @param id the username + * @return a list of all favorites if they exist + */ + List<Favourite> getAllByUsername(String name); + + /** + * Gets all favorites by recipe ID + * + * @param id the ID of the recipe + * @return a list of all favorites if they exist + */ + List<Favourite> getAllByRecipeId(long id); + +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/user/AchievementRepository.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/user/AchievementRepository.java new file mode 100644 index 0000000000000000000000000000000000000000..a46022982930ff292a248208c8bd5f91a8ef431f --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/repository/user/AchievementRepository.java @@ -0,0 +1,23 @@ +package ntnu.idatt2016.v233.SmartMat.repository.user; + +import ntnu.idatt2016.v233.SmartMat.entity.user.Achievement; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +/** + * Repository for achievements + * + * @author Anders + * @version 1.0 + * @since 19.04.2023 + */ +public interface AchievementRepository extends JpaRepository<Achievement, String> { + + /** + * Finds an achievement by its name + * @param achievementName the name of the achievement to look up + * @return an optional containing the achievement if it exists + */ + Optional<Achievement> findByAchievementName(String achievementName); +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/FavouriteService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/FavouriteService.java new file mode 100644 index 0000000000000000000000000000000000000000..f4e2d1a4064b82c9ae48d95154332b0881c288fe --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/FavouriteService.java @@ -0,0 +1,77 @@ +package ntnu.idatt2016.v233.SmartMat.service; + +import java.util.List; +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import ntnu.idatt2016.v233.SmartMat.dto.request.FavoriteRequest; +import ntnu.idatt2016.v233.SmartMat.entity.Favourite; +import ntnu.idatt2016.v233.SmartMat.repository.FavoriteRepository; + +/** + * Service for the favorites enitity + * + * @author Stian Lyng + * @version 1.1 + */ +@Service +public class FavouriteService { + + @Autowired + FavoriteRepository favoriteRepository; + + /** + * Create and save a shopping list to the database + * @param shoppingList the shopping list to save + * @return the saved shopping list + */ + public Favourite addFav(FavoriteRequest request) { + Favourite favourite = new Favourite(); + favourite.setRecipeId(request.getRecipeId()); + favourite.setUsername(request.getUsername()); + return favoriteRepository.save(favourite); + } + + /** + * Gets all favorites + * + * @return a list of all favorites if they exist + */ + public List<Favourite> getAllFavorites() { + return favoriteRepository.findAll(); + } + + /** + * Gets all favorites by recipe ID + * + * @param id the ID of the recipe + * @return a list of all favorites if they exist + */ + public List<Favourite> getAllFavoritesByRecipeId(long id) { + return favoriteRepository.getAllByRecipeId(id); + } + + /** + * Gets all favorites by username + * + * @param id the username of the user + * @return a list of all favorites if they exist + */ + public List<Favourite> getAllFavoritesByUsername(String name) { + return favoriteRepository.getAllByUsername(name); + } + + /** + * Deletes a favorite by its ID + * + * @param id the ID of the favorite entry + */ + public void deleteFavoriteById(long id) { + Optional<Favourite> favorite = favoriteRepository.findById(id); + if (favorite.isPresent()) { + favoriteRepository.deleteById(id); + } + } +} diff --git a/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/AchievementService.java b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/AchievementService.java new file mode 100644 index 0000000000000000000000000000000000000000..f0a20695d80938d6c2e28291b331f7dbb2099b1f --- /dev/null +++ b/src/main/java/ntnu/idatt2016/v233/SmartMat/service/user/AchievementService.java @@ -0,0 +1,44 @@ +package ntnu.idatt2016.v233.SmartMat.service.user; + +import lombok.AllArgsConstructor; +import ntnu.idatt2016.v233.SmartMat.entity.user.Achievement; +import ntnu.idatt2016.v233.SmartMat.repository.user.AchievementRepository; +import org.springframework.stereotype.Service; + +import java.util.Optional; + +/** + * Service for achievements + * + * @author Anders + * @version 1.0 + * @since 19.04.2023 + */ + +@Service +@AllArgsConstructor +public class AchievementService { + + private AchievementRepository achievementRepository; + + /** + * Adds an achievement to the database + * @param achievementName name of achievement to add + * @param achievementDescription description of achievement to add + */ + public void addAchievement(String achievementName, String achievementDescription){ + achievementRepository.save(Achievement.builder() + .achievementName(achievementName) + .achievementDescription(achievementDescription) + .build()); + } + + /** + * Gets an achievement from the database + * @param achievementName name of achievement to get + * @return an optional containing the achievement if it exists + */ + public Optional<Achievement> getAchievement(String achievementName){ + return achievementRepository.findByAchievementName(achievementName); + } +}