This includes user and no user routes! Now wired to the frontend, however, it will still create an engagement even if it fails...
130 lines
5.0 KiB
Go
130 lines
5.0 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
domain "github.com/haydenhargreaves/Potion/internal/domain/engagement"
|
|
domainRecipe "github.com/haydenhargreaves/Potion/internal/domain/recipe"
|
|
_ "github.com/lib/pq"
|
|
)
|
|
|
|
type EngagementService struct {
|
|
engagementRepository domain.EngagementRepository
|
|
recipeRepository domainRecipe.RecipeRepository
|
|
}
|
|
|
|
// Compile-time check to ensure the EngagementService implements domain.EngagementService
|
|
var _ domain.EngagementService = (*EngagementService)(nil)
|
|
|
|
// NewUserRepository creates a user repository object which is used by the user service to access
|
|
// the database. Any user related database operations will take place in this repository.
|
|
func NewEngagementService(engagementRepository domain.EngagementRepository, recipeRepository domainRecipe.RecipeRepository) domain.EngagementService {
|
|
return &EngagementService{
|
|
engagementRepository: engagementRepository,
|
|
recipeRepository: recipeRepository,
|
|
}
|
|
}
|
|
|
|
// ViewRecipe requires a user ID and a recipe ID to create an engagement record in the database.
|
|
// A message will be generated using the recipe data and then used to add a view engagement to the
|
|
// database.
|
|
func (s *EngagementService) ViewRecipe(recipeId int) (domain.Engagement, error) {
|
|
recipe, err := s.recipeRepository.GetRecipe(recipeId, nil)
|
|
if err != nil {
|
|
return domain.Engagement{}, err
|
|
}
|
|
|
|
message := fmt.Sprintf("Viewed \"%s\"", recipe.Title)
|
|
|
|
return s.engagementRepository.AddEntityEngagement(recipeId, message, domain.EngagementViewed)
|
|
}
|
|
|
|
// ShareRecipe requires a user ID and a recipe ID to create an engagement record in the database.
|
|
// A message will be generated using the recipe data and then used to add a view engagement to the
|
|
// database.
|
|
func (s *EngagementService) ShareRecipe(recipeId int) (domain.Engagement, error) {
|
|
recipe, err := s.recipeRepository.GetRecipe(recipeId, nil)
|
|
if err != nil {
|
|
return domain.Engagement{}, err
|
|
}
|
|
|
|
message := fmt.Sprintf("Shared \"%s\"", recipe.Title)
|
|
|
|
return s.engagementRepository.AddEntityEngagement(recipeId, message, domain.EngagementShared)
|
|
}
|
|
|
|
// UserViewRecipe requires a user ID and a recipe ID to create an engagement record in the database.
|
|
// A message will be generated using the recipe data and then used to add a view engagement to the
|
|
// database.
|
|
func (s *EngagementService) UserViewRecipe(userId, recipeId int) (domain.Engagement, error) {
|
|
recipe, err := s.recipeRepository.GetRecipe(recipeId, &userId)
|
|
if err != nil {
|
|
return domain.Engagement{}, err
|
|
}
|
|
|
|
message := fmt.Sprintf("Viewed \"%s\"", recipe.Title)
|
|
|
|
return s.engagementRepository.AddUserEntityEngagement(userId, recipeId, message, domain.EngagementViewed)
|
|
}
|
|
|
|
// UserFavoriteRecipe requires a user ID and a recipe ID to create an engagement record in the database.
|
|
// A message will be generated using the recipe data and then used to add a like engagement to the
|
|
// database.
|
|
func (s *EngagementService) UserFavoriteRecipe(userId, recipeId int) (domain.Engagement, error) {
|
|
recipe, err := s.recipeRepository.GetRecipe(recipeId, &userId)
|
|
if err != nil {
|
|
return domain.Engagement{}, err
|
|
}
|
|
|
|
// Update the favorites DB
|
|
liked, err := s.engagementRepository.UserFavoriteRecipeToggle(userId, recipeId)
|
|
if err != nil {
|
|
return domain.Engagement{}, err
|
|
}
|
|
|
|
// Determine if this like is a saving or unsaving
|
|
var message string
|
|
if liked {
|
|
message = fmt.Sprintf("Saved \"%s\"", recipe.Title)
|
|
} else {
|
|
message = fmt.Sprintf("Unsaved \"%s\"", recipe.Title)
|
|
}
|
|
|
|
return s.engagementRepository.AddUserEntityEngagement(userId, recipeId, message, domain.EngagementLiked)
|
|
|
|
}
|
|
|
|
// UserMakeRecipe requires a user ID and a recipe ID to create an engagement record in the database.
|
|
// A message will be generated using the recipe data and then used to add a make engagement to the
|
|
// database.
|
|
func (s *EngagementService) UserMakeRecipe(userId, recipeId int) (domain.Engagement, error) {
|
|
recipe, err := s.recipeRepository.GetRecipe(recipeId, &userId)
|
|
if err != nil {
|
|
return domain.Engagement{}, err
|
|
}
|
|
|
|
message := fmt.Sprintf("Made \"%s\"", recipe.Title)
|
|
|
|
return s.engagementRepository.AddUserEntityEngagement(userId, recipeId, message, domain.EngagementMade)
|
|
}
|
|
|
|
// UserShareRecipe requires a user ID and a recipe ID to create an engagement record in the database.
|
|
// A message will be generated using the recipe data and then used to add a make engagement to the
|
|
// database.
|
|
func (s *EngagementService) UserShareRecipe(userId, recipeId int) (domain.Engagement, error) {
|
|
recipe, err := s.recipeRepository.GetRecipe(recipeId, &userId)
|
|
if err != nil {
|
|
return domain.Engagement{}, err
|
|
}
|
|
|
|
message := fmt.Sprintf("Shared \"%s\"", recipe.Title)
|
|
|
|
return s.engagementRepository.AddUserEntityEngagement(userId, recipeId, message, domain.EngagementShared)
|
|
}
|
|
|
|
// GetUserEngagement returns a list of the users most recent engagement entries. The number of records
|
|
// is determined by the limit passed into this function. The results are sorted, newest-to-oldest.
|
|
func (s *EngagementService) GetUserEngagement(userId, limit int) ([]domain.Engagement, error) {
|
|
return s.engagementRepository.GetUserEngagement(userId, limit)
|
|
}
|