using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using System.Security.Claims; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using MyRecipes2.Server.Data; using MyRecipes2.Server.Models; using MyRecipes2.Shared; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using static MyRecipes2.Server.Controllers.RecipeController; namespace MyRecipes2.Server.Controllers { [ApiController] public class SubscriptionController : ControllerBase { private readonly ApplicationDbContext dbContext; public SubscriptionController(ApplicationDbContext dbContext) { this.dbContext = dbContext; } // returns true if user is subcribed to other user [HttpGet] [Route("[controller]/CheckSubscription/{subscribeFromId}/{subscribeToId}")] public bool Get(string subscribeFromId, string subscribeToId) { var s = dbContext.Subscriptions .Where(x => x.Subscribed.Id == subscribeToId && x.Subscriber.Id == subscribeFromId) .ToList(); if (s.Count() > 0) { return true; } return false; } // returns list of users that person is subsribed to [HttpGet] [Route("[controller]/GetSubscriptions/{userId}")] public List<RecipeDto> Get(string userId) { // goal - return a list of sorted recipes that have been newly added or changed // Get the list of subscriptions var subscriptions = dbContext.Subscriptions .Where(x => x.Subscriber.Id == userId) .Include(s => s.Subscribed) .ToList(); // for each subscription // loop through the list of recipes // check if last modified date of recipe is > subscription date // add to the overall list var recipeList = new List<RecipeDto>(); if (subscriptions.Count() > 0) { var recipeController = new RecipeController(dbContext); foreach (var subscription in subscriptions) { var recipes = recipeController.GetUserRecipies(subscription.Subscribed.Id); Console.WriteLine($"recipes found: {recipes.Count()}"); foreach(var recipe in recipes) { Console.WriteLine($"{recipe.Name} {recipe.LastModified} {subscription.TimeSubscribed}"); if (DateTime.Compare(recipe.LastModified, subscription.TimeSubscribed) > 0) { recipeList.Add(recipe); } } } } Console.WriteLine($"recipes added: {recipeList.Count()}"); Console.WriteLine(DateTime.UtcNow); // Generate Hashtable (subscribed to id -> number of likes) // loop through all likes // Table[likes.recipe.author] += 1 if liker == current user var totalLikeCount = new Dictionary<string, int>(); foreach (var subscription in subscriptions) { totalLikeCount[subscription.Subscribed.Id] = 0; } var likes = dbContext.Likes .Where(l => l.LikedBy.Id == userId) .Include(l => l.Recipe.CreatedBy); /* Console.WriteLine($"Created by {likes.First().Recipe.Calories}"); Console.WriteLine($"Num likes: {likes.Count()}"); foreach (var like in likes) { var id = like.Recipe.CreatedBy.Id; if (totalLikeCount.ContainsKey(id)) { totalLikeCount[id] += 1; } } */ // sort by - recipe creation date - total number of likes explorer has for recipes contributer profile - recipe creation time recipeList .OrderBy(r => r.CreatedDateTime.Date) //.ThenBy(r => totalLikeCount[r.Author_id]) .ThenBy(r => r.CreatedDateTime.TimeOfDay); return recipeList; } [HttpPost] [Route("[controller]")] public void Post(SubscriptionRequestDto request) { var subscriber = dbContext.Users .Where(x => x.Id.Equals(request.subscribeFromId)) .First(); var subscription = dbContext.Users .Where(x => x.Id.Equals(request.subscribeToId)) .First(); Subscription s = new(); s.Subscriber = subscriber; s.Subscribed = subscription; s.TimeSubscribed = DateTime.UtcNow; dbContext.Subscriptions.Add(s); dbContext.SaveChanges(); return ; } [HttpDelete] [Route("[controller]/DeleteSubscription/{subscribeFromId}/{subscribeToId}")] public void Delete(string subscribeFromId, string subscribeToId) { Subscription s = dbContext.Subscriptions .Where(x => x.Subscribed.Id == subscribeToId && x.Subscriber.Id == subscribeFromId) .First(); dbContext.Subscriptions.Remove(s); dbContext.SaveChanges(); } [HttpGet] [Route("[controller]/SubscriptionItem/{authorId}")] public SubscriptionItemDto GetSubscriptionItem (string authorId) { var subscribedAuthor = dbContext.Users .Where(u => u.Id == authorId) .First(); return new SubscriptionItemDto(subscribedAuthor.Id, subscribedAuthor.UserName); } [HttpGet] [Route("[controller]/SubscriptionList/{userId}")] public List<SubscriptionItemDto> GetUserSubscription (String userId) { var subscriptionsDb = dbContext.Subscriptions .Where(x => x.Subscriber.Id == userId) .Include(s => s.Subscribed) .ToList(); List<SubscriptionItemDto> subscriptions = new List<SubscriptionItemDto>(); foreach (var s in subscriptionsDb) { subscriptions.Add(GetSubscriptionItem(s.Subscribed.Id)); } return subscriptions; } } }