public ActionResult Speaker(string conferenceSlug, CfpSpeaker speaker, HttpPostedFileBase imageFile) { var conference = _conferenceService.GetConferenceBySlug(conferenceSlug); var userId = User.Identity.GetUserId(); if (ModelState.IsValid) { try { var dbSpeaker = _dbContext.CfpSpeakers.SingleOrDefault(sp => sp.UserId == userId && sp.ConferenceId == conference.Id); if (dbSpeaker == null) { if (!User.IsAllowedToCreateCfp(conference)) { // CFP Isn't open. Can't create new speakers. return RedirectToAction("Index"); } dbSpeaker = new CfpSpeaker(); _dbContext.Entry(dbSpeaker).State = EntityState.Added; dbSpeaker.UserId = userId; } dbSpeaker.ConferenceId = conference.Id; dbSpeaker.Bio = speaker.Bio; dbSpeaker.Company = speaker.Company; dbSpeaker.Email = speaker.Email; dbSpeaker.GitHub = speaker.GitHub; dbSpeaker.Name = speaker.Name; dbSpeaker.Twitter = speaker.Twitter; dbSpeaker.Web = speaker.Web; dbSpeaker.Comments = speaker.Comments; dbSpeaker.NeedTravel = speaker.NeedTravel; dbSpeaker.NeedAccommodation = speaker.NeedAccommodation; if (imageFile != null && imageFile.ContentLength > 0) { if (imageFile.ContentLength < 10*1024*1024) { try { using (var memStream = new MemoryStream()) { imageFile.InputStream.CopyTo(memStream); var imageUrl = _imageUploader.UploadToStorage(memStream, dbSpeaker.Email, _cfpSpeakerImageContainerName); dbSpeaker.Image = imageUrl; } } catch (ImageUploadException e) { ModelState.AddModelError("ImageFile", e.Message); } } else { ModelState.AddModelError("ImageFile", "Max file size of image is ~10MB"); } } // If there are still no errors if (ModelState.IsValid) { _dbContext.SaveChanges(); return RedirectToAction("Conference"); } } catch (Exception ex) { ModelState.AddModelError("Saving", ex); } } ViewBag.Conference = conference; return View(speaker); }
public ActionResult Speaker(string conferenceSlug) { var conference = _conferenceService.GetConferenceBySlug(conferenceSlug); var userId = User.Identity.GetUserId(); var speaker = _dbContext.CfpSpeakers.Include(sp => sp.Sessions).SingleOrDefault(sp => sp.UserId == userId && sp.ConferenceId == conference.Id); if (speaker == null) { if (!User.IsAllowedToCreateCfp(conference)) { // CFP Isn't open. Can't create new speakers. return RedirectToAction("Index"); } speaker = new CfpSpeaker() { Email = User.Identity.Name }; } ViewBag.Conference = conference; return View(speaker); }