public int CreateArc(CreateSeriesRelatedEntityServiceModel model) { var series = this.dbContext.Series.Find(model.SeriesId); if (series == null) { throw new KeyNotFoundException($"Series with given id {model.SeriesId} does not exist"); } if (series.Arcs.Any(a => a.Number == model.Number)) { throw new InvalidOperationException( $"Cannot insert another {typeof(Arc).Name} with the same number"); } 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 newArc = new Arc { Title = model.Title, Number = model.Number, Description = model.Description, CoverPath = model.CoverPath, SeriesId = model.SeriesId, Genres = selectedGenres, }; this.dbContext.Arcs.Add(newArc); this.dbContext.SaveChanges(); return(newArc.Id); }
public async Task <IActionResult> Create(CreateSeriesRelatedEntityInputModel model) { if (!this.ModelState.IsValid) { model.RetrievedGenres = this.genreRetrievalService.GetAllAsKeyValuePairs(); return(this.View(model)); } var serviceModel = new CreateSeriesRelatedEntityServiceModel { Title = model.Title, Number = model.Number, CoverImage = await model.CoverImage.GetBytes(), CoverPath = model.CoverPath, Description = model.Description, Genres = model.Genres, SeriesId = model.SeriesId, RetrievedGenres = model.RetrievedGenres, }; try { var id = this.issueCreationService.CreateIssue(serviceModel); this.cache.RemoveSeriesDetails(model.SeriesId); return(this.Redirect($"/Issue/{id}")); } catch (KeyNotFoundException) { return(this.NotFound()); } catch (InvalidOperationException) { return(this.BadRequest()); } }
public int CreateArc(CreateSeriesRelatedEntityServiceModel model) { var series = this.dbContext.Series .Include(s => s.Arcs) .FirstOrDefault(s => s.Id == model.SeriesId); if (series == null) { throw new KeyNotFoundException($"Series with given id {model.SeriesId} does not exist"); } if (series.Arcs.Any(a => a.Number == model.Number)) { throw new InvalidOperationException( $"Cannot insert another {typeof(Arc).Name} with the same number"); } var selectedGenres = new List <Genre>(); if (model.Genres != null) { selectedGenres = this.dbContext.Genres .ToList() .Where(g => model.Genres.Any(x => x == g.Id)) .ToList(); } Arc newArc = null; if (model.CoverImage == null) { newArc = new Arc { Title = model.Title, Number = model.Number, Description = model.Description, CoverPath = model.CoverPath, SeriesId = model.SeriesId, Genres = selectedGenres, }; } else { var uniqueFileName = this.fileUploadService.GetUploadedFileName(model.CoverImage, model.Title); newArc = new Arc { Title = model.Title, Number = model.Number, Description = model.Description, CoverPath = uniqueFileName, SeriesId = model.SeriesId, Genres = selectedGenres, }; } this.dbContext.Arcs.Add(newArc); this.dbContext.SaveChanges(); return(newArc.Id); }