Skip to content
Snippets Groups Projects
Commit b44076aa authored by Birk Øvstetun Narvhus's avatar Birk Øvstetun Narvhus
Browse files

Merge branch 'main' into 'test/75-test-for-repos'

Main

See merge request idatt2106-v23-03/backend!48
parents 05bf070a 93db5a8f
No related branches found
No related tags found
No related merge requests found
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;
}
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;
}
...@@ -15,7 +15,7 @@ import lombok.NoArgsConstructor; ...@@ -15,7 +15,7 @@ import lombok.NoArgsConstructor;
@Data @Data
public class Category { public class Category {
@Id @Id
@Column(name = "cateogry_name") @Column(name = "category_name")
String ean; String ean;
@Column(name = "category_description") @Column(name = "category_description")
String description; String description;
......
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;
}
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);
}
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);
}
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);
}
}
}
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);
}
}
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