public UserFavoriteAndReadResponseModel GetFavAndReads(Guid id) { UserFavoriteAndReadResponseModel model = new UserFavoriteAndReadResponseModel(); List <UserFavoriteAndReadModel> reads = new List <UserFavoriteAndReadModel>(); List <UserFavoriteAndReadModel> favorites = new List <UserFavoriteAndReadModel>(); List <UserFavoritAndReadBook> entities = _userFavoriteAndReadBook.GetList(x => x.UserId == id); List <Book> books = _books.GetList(); List <AppFile> files = _appFile.GetList(); List <Author> authors = _authors.GetList(); foreach (var item in entities.Where(x => x.Type == UserFavAndRead.Reads)) { UserFavoriteAndReadModel readModel = new UserFavoriteAndReadModel(); var entity = books.FirstOrDefault(x => x.Id == item.BookId); if (entity != null) { readModel.BookId = entity.Id; readModel.UserId = item.UserId; readModel.AuthorId = entity.AuthorId; readModel.AuthorName = authors.FirstOrDefault(x => x.Id == entity.AuthorId).Name; readModel.AuthorSurname = authors.FirstOrDefault(x => x.Id == entity.AuthorId).Surname; readModel.SignUrl = "https://elibrarystorage.blob.core.windows.net/" + files.FirstOrDefault(x => x.ModuleId == entity.Id).BlobPath; reads.Add(readModel); } } foreach (var item in entities.Where(x => x.Type == UserFavAndRead.Favorite)) { UserFavoriteAndReadModel favoriteModel = new UserFavoriteAndReadModel(); var entity = books.FirstOrDefault(x => x.Id == item.BookId); if (entity != null) { favoriteModel.BookId = entity.Id; favoriteModel.UserId = item.UserId; favoriteModel.AuthorId = entity.AuthorId; favoriteModel.AuthorName = authors.FirstOrDefault(x => x.Id == entity.AuthorId).Name; favoriteModel.AuthorSurname = authors.FirstOrDefault(x => x.Id == entity.AuthorId).Surname; favoriteModel.SignUrl = files.FirstOrDefault(x => x.ModuleId == entity.Id).BlobPath; favorites.Add(favoriteModel); } } model.Favorites = favorites; model.Reads = reads; return(model); }
public async Task <ActionResult> Favorite([FromBody] UserFavoriteAndReadModel model) { //UsersFavoritesAndReads read = new UsersFavoritesAndReads(); //MongoBook mongoBookModel = new MongoBook(); //var filter = Builders<UsersFavoritesAndReads>.Filter.Eq("UserId", model.UserId); UserFavoritAndReadBook favorite = new UserFavoritAndReadBook(); try { var entity = await _userFavoriteAndReadBook.GetListAsync(x => x.UserId == model.UserId); if (entity.Count == 0) { favorite.UserId = model.UserId; favorite.BookId = model.BookId; favorite.Type = UserFavAndRead.Favorite; await _userFavoriteAndReadBook.AddAsync(favorite); } else { if (entity.FirstOrDefault(x => x.BookId == model.BookId && x.Type == UserFavAndRead.Favorite) == null) { favorite.UserId = model.UserId; favorite.BookId = model.BookId; favorite.Type = UserFavAndRead.Favorite; await _userFavoriteAndReadBook.AddAsync(favorite); } else { var deletingEntity = entity.FirstOrDefault(x => x.BookId == model.BookId); _userFavoriteAndReadBook.Delete(deletingEntity); } } } catch (Exception e) { return(StatusCode(404)); } return(StatusCode(200)); }