public async Task <IActionResult> CreateEpisode(int id, [FromForm] EpisodeRequest model) { var movie = _movieRepository.FindById(id); if (!ModelState.IsValid || movie == null || model.File == null) { return(BadRequest(ErrorResponse.InvalidPayload)); } var episode = new EpisodeEntity() { Name = model.Name, MovieId = movie.Id, }; var filePath = $"/{DateTime.Now.ToFileTime()}_{model.File.FileName}"; using (var stream = new FileStream($"wwwroot/{filePath}", FileMode.Create)) { await model.File.CopyToAsync(stream); episode.Url = filePath; } _episodeRepository.Add(episode); await _unitOfWork.SaveChangesAsync(); return(Ok()); }
public async Task <IEntityCreateResult> CreateEpisodeAsync(ICreateEpisodeModel createModel) { if (createModel is null) { throw new ArgumentNullException(nameof(createModel)); } await _createEpisodeValidator.ValidateAndThrowAsync(createModel); Episode episodeToAdd = _createEpisodeFactory.Create(createModel); IEntityCreateResult identity = _episodeRepository.Add(episodeToAdd); return(identity); }
public async Task <IActionResult> Store(EpisodeCreateViewModel model) { try { if (ModelState.IsValid) { var imageName = GetUniqueFileName(model.Image.FileName); var uploads = Path.Combine(_environment.WebRootPath, "uploads"); var filePath = Path.Combine(uploads, imageName); using (var steam = System.IO.File.Create(filePath)) { await model.Image.CopyToAsync(steam); } var thumb = GetUniqueFileName(model.Thumb.FileName); uploads = Path.Combine(_environment.WebRootPath, "uploads"); filePath = Path.Combine(uploads, thumb); using (var steam = System.IO.File.Create(filePath)) { await model.Thumb.CopyToAsync(steam); } Episode episode = new Episode { Title = model.Title, Description = model.Description, Image = imageName, Thumb = thumb, VidelUrl = model.VidelUrl, CreatedAt = DateTime.Now }; _episodeRepository.Add(episode); _episodeRepository.SaveChanges(); TempData["Success"] = "Episódio cadastrado com sucesso!"; return(RedirectToAction("Index")); } } catch (Exception exception) { TempData["Error"] = "Erro ao cadastrar episódio"; _logger.LogError("Episode store error: " + exception); } return(View("Create", model)); }