Пример #1
0
        public IActionResult Create(
            Guid personGuid,
            PersonFluffyDateCreateModel createModel)
        {
            // Must have GUID
            if (personGuid == Guid.Empty)
            {
                return(NotFound());
            }

            // Must have a person
            var person =
                _personData.ReadPersonWithFluffyDates(personGuid);

            // Do we have what we need so far?
            if (person == null)
            {
                return(NotFound());
            }

            // Page navigation
            ViewData["PersonGuid"] = person.PersonGuid;

            //
            // Validate
            //

            var form =
                new PersonFluffyDateModelState(
                    ModelState,
                    new FluffyDate(
                        createModel.Year,
                        createModel.Month,
                        createModel.Day),
                    createModel.Type,
                    person.FluffyDates,
                    null);

            form.HasValidValues();
            if (!ModelState.IsValid)
            {
                return(View(createModel));
            }

            form.HasUniqueTypeToBeCreated();
            if (!ModelState.IsValid)
            {
                return(View(createModel));
            }

            form.BirthVsDeathYear();
            if (!ModelState.IsValid)
            {
                return(View(createModel));
            }

            form.BirthVsDeathMonth();
            if (!ModelState.IsValid)
            {
                return(View(createModel));
            }

            form.BirthVsDeathDay();
            if (!ModelState.IsValid)
            {
                return(View(createModel));
            }

            form.PersonHasNormalAge();
            if (!ModelState.IsValid)
            {
                return(View(createModel));
            }

            form.DateIsNotInTheFuture(
                DateTime.Now.Year,
                DateTime.Now.Month,
                DateTime.Now.Day);
            if (!ModelState.IsValid)
            {
                return(View(createModel));
            }

            //
            // Create
            //

            var newPersonFluffyDate = new PersonFluffyDate();

            newPersonFluffyDate.PersonId = person.PersonId;
            newPersonFluffyDate.Year     = createModel.Year;
            newPersonFluffyDate.Month    = createModel.Month;
            newPersonFluffyDate.Day      = createModel.Day;
            newPersonFluffyDate.Type     =
                (PersonFluffyDateType)createModel.Type;

            try
            {
                _personData.AddPersonFluffyDate(newPersonFluffyDate);
            }
            catch (DbUpdateException /* ex */)
            {
                // Log the error (uncomment ex variable name and write a log.)
                ModelState.AddModelError(
                    "",
                    "Unable to save changes. " +
                    "Try again, and if the problem persists, " +
                    "see your system administrator.");

                return(View(createModel));
            }

            //
            // Update RDF
            //

            var readPerson = _personData.ReadAllPersonData(personGuid);

            if (readPerson != null)
            {
                _rdfData.AddOrUpdatePerson(readPerson);
            }

            //
            // Update search index
            //

            // Update selected year type
            switch (createModel.Type)
            {
            case PersonFluffyDateTypeOptions.Birth:
                _personSearchIndex.MergeYearOfBirth(
                    person.PersonGuid.ToString(),
                    createModel.Year);
                break;

            case PersonFluffyDateTypeOptions.Death:
                _personSearchIndex.MergeYearOfDeath(
                    person.PersonGuid.ToString(),
                    createModel.Year);
                break;
            }

            // Update age

            // Get updated dates
            person = _personData.ReadPersonWithFluffyDates(personGuid);

            var birthFluffyDate =
                person.FluffyDates.FirstOrDefault(x =>
                                                  x.Type == PersonFluffyDateType.Birth);

            if (birthFluffyDate?.Year != null)
            {
                var fluffyDateStart =
                    new FluffyDate(
                        birthFluffyDate.Year,
                        birthFluffyDate.Month,
                        birthFluffyDate.Day);

                var fluffyDateEnd =
                    new FluffyDate(
                        DateTime.Now.Year,
                        DateTime.Now.Month,
                        DateTime.Now.Day);

                var deathFluffyDate =
                    person.FluffyDates.FirstOrDefault(x =>
                                                      x.Type == PersonFluffyDateType.Death);

                if (deathFluffyDate?.Year != null)
                {
                    fluffyDateEnd =
                        new FluffyDate(
                            deathFluffyDate.Year,
                            deathFluffyDate.Month,
                            deathFluffyDate.Day);
                }

                var age =
                    new Age(
                        fluffyDateStart,
                        fluffyDateEnd);

                if (age.InYears != null)
                {
                    _personSearchIndex.MergeAge(
                        person.PersonGuid.ToString(),
                        age.InYears);
                }
            }

            //
            // Redirect
            //

            return(RedirectToAction(
                       "Details",
                       "Person",
                       new { personGuid = personGuid }));
        }