public async Task <IActionResult> GetWatchlist([FromRoute] Guid id, [FromQuery] Guid userId) { var watchlistEntity = await _repo.GetWatchlistByUserIdAsync(userId); if (watchlistEntity == null) { watchlistEntity = new Entities.Watchlist() { Userid = userId, Id = id, Name = "WatchLaterList" }; } if (!watchlistEntity.Id.Equals(id)) { return(BadRequest("Given watchlist does not belong to given user.")); } var result = _mapper.Map <Model.WatchlistModel>(watchlistEntity); //result.Medias = _mapper.Map<IEnumerable<Guid>>(watchlistEntity.Medias).ToList(); var link = new Model.LinkModel() { type = "GET", rel = "data", href = string.Format(_configuration["AppSettings:MediaServiceUrl"], watchlistEntity.MediaIdsToString()) }; result.Link.Add(link); return(Ok(result)); }
public async Task <IActionResult> AddUser([FromBody] Model.UserForCreation userToAdd) { var userEntity = _mapper.Map <Entities.User>(userToAdd); var userWatchlater = new Entities.Watchlist(userToAdd.UserName); userEntity.WatchLaterId = userWatchlater.Id; userEntity.WatchLater = userWatchlater; _watchlistRepo.AddWatchlist(userWatchlater); _repo.AddUser(userEntity); await _repo.SaveChangesAsync(); var resultEntity = await _repo.GetUserAsync(userEntity.Id); var result = _mapper.Map <Model.UserModel>(resultEntity); return(CreatedAtRoute("GetUser", new { id = result.Id }, result)); }
public void UpdateWatchlist(Entities.Watchlist watchlistToUpdate) { _context.Watchlists.Update(watchlistToUpdate); }
public void AddWatchlist(Entities.Watchlist watchlistToAdd) { _context.Watchlists.AddAsync(watchlistToAdd); }
public async Task <IActionResult> AddWatchlistMediaEntry([FromBody] Model.WatchlistMediaEntryForCreation entry) { //Check if watchlist belongs to user var watchlistEntity = await _repo.GetWatchlistByUserIdAsync(entry.UserId); bool isNewList = false; if (watchlistEntity == null) { watchlistEntity = new Entities.Watchlist() { Userid = entry.UserId, Id = entry.WatchlistId, Name = "WatchLaterList" }; isNewList = true; } else if (!watchlistEntity.Id.Equals(entry.WatchlistId)) { return(BadRequest("Given watchlist does not belong to given user")); } //Check if the entry exists var watchlistEntryEntity = await _repo.GetWatchListMediaEntryAsync(entry.WatchlistId, entry.MediaId); if (watchlistEntryEntity != null) { return(NoContent()); //If entry exists it means the operation was done successfully, just return No content. } else { watchlistEntryEntity = _mapper.Map <Entities.WatchlistMedia>(entry); //If entry doesn't exists, map entry to a new entity } //Add to watchlist watchlistEntity.Medias.Add(watchlistEntryEntity); _repo.UpdateWatchlist(watchlistEntity); var success = await _repo.SaveChangesAsync(); if (!success) { return(new StatusCodeResult(500)); } //Map Watchlist with updated entry var listResult = _mapper.Map <Model.WatchlistModel>(watchlistEntity); var link = new Model.LinkModel() { type = "GET", rel = "data", href = string.Format(_configuration["AppSettings:MediaServiceUrl"], watchlistEntity.MediaIdsToString()) }; listResult.Link.Add(link); //Return result if (isNewList) { return(Created($"api/watchlists/{watchlistEntity.Id}", listResult)); } else { return(Ok(listResult)); } }