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

deleted favorite entity/repo/service

parent 5956fed3
No related branches found
No related tags found
No related merge requests found
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;
}
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.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);
}
}
}
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