// GET: Products/Create public IActionResult Create() { var model = new VotingEventViewModel { StartDate = DateTime.Today, EndDate = DateTime.Today }; return(View(model)); }
private VotingEvent ToVotingEvent(VotingEventViewModel view, string path) { return(new VotingEvent { Id = view.Id, Name = view.Name, Description = view.Description, StartDate = view.StartDate, EndDate = view.EndDate, ImageUrl = path, User = view.User }); }
public async Task <IActionResult> Edit(VotingEventViewModel model) { if (ModelState.IsValid) { try { var path = model.ImageUrl; if (model.ImageFile != null && model.ImageFile.Length > 0) { var guid = Guid.NewGuid().ToString(); var file = $"{guid}.jpg"; path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot\\images\\VotingEvents", file); using (var stream = new FileStream(path, FileMode.Create)) { await model.ImageFile.CopyToAsync(stream); } path = $"~/images/VotingEvents/{file}"; } var votingEvent = this.ToVotingEvent(model, path); // TODO: Pending to change to: this.User.Identity.Name votingEvent.User = await this.userHelper.GetUserByEmailAsync("*****@*****.**"); await this.votingEventRepository.UpdateAsync(votingEvent); } catch (DbUpdateConcurrencyException) { if (!await this.votingEventRepository.ExistAsync(model.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(model)); }
public async Task <IActionResult> Create(VotingEventViewModel model) { if (ModelState.IsValid) { var path = string.Empty; if (model.ImageFile != null && model.ImageFile.Length > 0) { var guid = Guid.NewGuid().ToString(); var file = $"{guid}.jpg"; path = Path.Combine( Directory.GetCurrentDirectory(), "wwwroot\\images\\VotingEvents", file); using (var stream = new FileStream(path, FileMode.Create)) { await model.ImageFile.CopyToAsync(stream); } path = $"~/images/VotingEvents/{file}"; } var votingEvent = this.ToVotingEvent(model, path); votingEvent.User = await this.userHelper.GetUserByEmailAsync("*****@*****.**"); await this.votingEventRepository.CreateAsync(votingEvent); return(RedirectToAction(nameof(Index))); // TODO: Pending to change to: this.User.Identity.Name //votingEvent.User = await this.userHelper.GetUserByEmailAsync("*****@*****.**"); //await this.votingEventRepository.CreateAsync(votingEvent); //return RedirectToAction(nameof(Index)); } return(View(model)); }