YouConf.Controllers.ConferenceController.Create C# (CSharp) Метод

Create() приватный Метод

private Create ( Conference conference ) : System.Web.Mvc.ActionResult
conference YouConf.Common.Data.Entities.Conference
Результат System.Web.Mvc.ActionResult
        public ActionResult Create(Conference conference)
        {
            if (!IsConferenceHashTagAvailable(conference.HashTag))
            {
                ModelState.AddModelError("HashTag", "Unfortunately that hashtag is not available.");
            }

            if (ModelState.IsValid)
            {
                var conferenceTimeZone = TimeZoneInfo.FindSystemTimeZoneById(conference.TimeZoneId);
                conference.StartDate = TimeZoneInfo.ConvertTimeToUtc(conference.StartDate, conferenceTimeZone);
                conference.EndDate = TimeZoneInfo.ConvertTimeToUtc(conference.EndDate, conferenceTimeZone);

                var currentUserProfile = YouConfDbContext.UserProfiles.FirstOrDefault(x => x.UserName == User.Identity.Name);
                conference.Administrators.Add(currentUserProfile);

                YouConfDbContext.Conferences.Add(conference);
                YouConfDbContext.SaveChanges();

                UpdateConferenceInSolrIndex(conference.Id, Common.Messaging.SolrIndexAction.Update);

                return RedirectToAction("Details", new { hashTag = conference.HashTag });
            }
            return View(conference);
        }

Same methods

ConferenceController::Create ( ) : System.Web.Mvc.ActionResult

Usage Example

        public void Create_WithAlreadyUsedHashTag_Should_ReturnViewAndAddModelError()
        {
            _context.Conferences.Add(new Conference() { HashTag = "abcde", Name = "test", Abstract = "test", StartDate = DateTime.Now, EndDate = DateTime.Now, TimeZoneId = "test", AvailableToPublic = false });
            _context.SaveChangesWithErrors();

            var conferenceController = new ConferenceController(_context);

            var newConference = new Conference() { HashTag = "abcde" };
            var result = conferenceController.Create(newConference)
                .As<ViewResult>();

            result.ViewData.ModelState["HashTag"]
                .Errors
                .Count
                .Should()
                .Be(1);
        }