public IActionResult ProvideImage(Guid projectId) { Project project = null; try { project = trainingApi.GetProject(projectId); } catch (Exception ex) { TempData["Error"] = $"Something went wrong."; } if (project == null) { TempData["Error"] += $"<br />Project with ID:<b>{projectId}</b> doesn't exists."; return(RedirectToAction(nameof(PlantsController.SendPhoto), "Plants")); } ProvideImageViewModel vm = new ProvideImageViewModel() { ProjectId = projectId, ProjectName = project.Name, TagsSelectList = new SelectList(trainingApi.GetTags(projectId), "Id", "Name"), //Tags = trainingApi.GetTags(projectId), }; return(View(vm)); }
public async Task <IActionResult> ProvideImage(Guid projectId, IFormFile image, ProvideImageViewModel provideImageViewModel) { if (!ModelState.IsValid) { if (provideImageViewModel.TagId == null) { TempData["Warning"] = "You must choose a tag for the photo."; } return(RedirectToAction(nameof(ImageController.Create), new { projectId })); } string imageId = null; try { if (image == null) { throw new Exception("Choose an image to send."); } using (var stream = image.OpenReadStream()) { imageId = await _imageStorageService.SaveImageAsync(stream, image.FileName); } } catch (Exception ex) { TempData["Error"] = ex.Message; return(RedirectToAction(nameof(ImageController.Create), new { projectId })); } ImageWaitingToConfirm imageWaitingToConfirm = new ImageWaitingToConfirm() { ImageWaitingToConfirmId = Guid.NewGuid(), ImageId = imageId, MyProjectId = projectId, MyTagId = Guid.Parse(provideImageViewModel.TagId), ProvidedBy = _userManager.GetUserId(HttpContext.User), SendTime = DateTime.UtcNow }; _context.ImagesWaitingToConfirm.Add(imageWaitingToConfirm); _context.SaveChanges(); TempData["Success"] = "Image send succesfully."; return(RedirectToAction(nameof(PlantsController.SendPhoto), "Plants")); }