The database requirements have been added, as well as the service/repo architecture. A few small functions have been created, but the system is not complete by any means. More work is required to mark this task complete.
61 lines
2.4 KiB
Go
61 lines
2.4 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,
|
|
}
|
|
}
|
|
|
|
// 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)
|
|
if err != nil {
|
|
return domain.Engagement{}, err
|
|
}
|
|
|
|
message := fmt.Sprintf("Viewed \"%s\"", recipe.Title)
|
|
|
|
return s.engagementRepository.AddUserEntityEngagement(userId, recipeId, message, domain.EngagementViewed)
|
|
}
|
|
|
|
// UserLikeRecipe 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) UserLikeRecipe(userId, recipeId int) (domain.Engagement, error) {
|
|
recipe, err := s.recipeRepository.GetRecipe(recipeId)
|
|
if err != nil {
|
|
return domain.Engagement{}, err
|
|
}
|
|
|
|
message := fmt.Sprintf("Liked \"%s\"", recipe.Title)
|
|
|
|
return s.engagementRepository.AddUserEntityEngagement(userId, recipeId, message, domain.EngagementLiked)
|
|
}
|
|
|
|
// 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)
|
|
}
|