public int EditArc(EditSeriesRelatedEntityServiceModel model) { var selectedGenres = new List <Genre>(); if (model.Genres != null) { selectedGenres = this.dbContext.Genres .ToList() .Where(g => model.Genres.Any(x => x == g.Id)) .ToList(); } var currentArc = this.dbContext.Arcs.Include(a => a.Genres).FirstOrDefault(a => a.Id == model.Id); if (currentArc == null) { throw new KeyNotFoundException($"Arc with given id {model.Id} does not exist"); } var series = this.dbContext.Series .Select(s => new { s.Id, s.Arcs, }) .FirstOrDefault(s => s.Id == model.SeriesId); if (series == null || currentArc.SeriesId != series.Id) { throw new KeyNotFoundException("Wrong series id given for arc."); } if (currentArc.Number != model.Number && series.Arcs.Any(a => a.Number == model.Number)) { throw new InvalidOperationException( $"Cannot insert another {typeof(Arc).Name} with the same number"); } currentArc.Title = model.Title; currentArc.Description = model.Description; currentArc.Number = model.Number; currentArc.Genres = selectedGenres; // else if -> Only updates thumbnail if data is passed. if (model.CoverImage != null) { var uniqueFileName = this.fileUploadService.GetUploadedFileName(model.CoverImage, model.Title); // Delete old cover image and replace it with the new one. this.fileUploadService.DeleteCover(currentArc.CoverPath); currentArc.CoverPath = uniqueFileName; } else if (model.CoverPath != null) { currentArc.CoverPath = model.CoverPath; } this.dbContext.Update(currentArc); this.dbContext.SaveChanges(); return(currentArc.Id); }
public async Task <IActionResult> Edit(EditSeriesRelatedEntityInputModel model) { if (!this.ModelState.IsValid) { model.RetrievedGenres = this.genreRetrievalService.GetAllAsKeyValuePairs(); return(this.View(model)); } var serviceModel = new EditSeriesRelatedEntityServiceModel { Id = model.Id, Title = model.Title, Number = model.Number, Description = model.Description, SeriesId = model.SeriesId, CoverImage = await model.CoverImage.GetBytes(), CoverPath = model.CoverPath, Genres = model.Genres, }; try { var id = this.issueEditingService.EditIssue(serviceModel); this.cache.RemoveIssueDetails(id); this.cache.RemoveAllArcDetails(this.cacheKeyHolder); this.cache.RemoveAllVolumeDetails(this.cacheKeyHolder); this.cache.RemoveSeriesDetails(model.SeriesId); return(this.Redirect($"/Issue/{id}")); } catch (KeyNotFoundException) { return(this.NotFound()); } catch (InvalidOperationException) { return(this.BadRequest()); } }