예제 #1
0
        public IActionResult Edit(int personFluffyDateId)
        {
            // Get data
            var fluffyDate =
                _personData
                .ReadPersonFluffyDateWithNavigation(personFluffyDateId);

            if (fluffyDate == null)
            {
                return(NotFound());
            }

            // Load data to model
            var editModel = new PersonFluffyDateEditModel();

            editModel.Year  = fluffyDate.Year;
            editModel.Month = fluffyDate.Month;
            editModel.Day   = fluffyDate.Day;
            editModel.Type  = (PersonFluffyDateTypeOptions)fluffyDate.Type;

            // Page navigation
            ViewData["PersonGuid"] = fluffyDate.Person.PersonGuid;

            return(View(editModel));
        }
예제 #2
0
        public IActionResult Edit(
            int personFluffyDateId,
            PersonFluffyDateEditModel editModel)
        {
            var fluffyDate =
                _personData
                .GetPersonFluffyDateWithNavigation(
                    personFluffyDateId);

            if (fluffyDate == null)
            {
                return(NotFound());
            }

            // Read to validate
            var person =
                _personData
                .ReadPersonWithFluffyDates(
                    fluffyDate.Person.PersonGuid);

            if (person == null)
            {
                return(NotFound());
            }

            // Page navigation
            ViewData["PersonGuid"] =
                fluffyDate.Person.PersonGuid;

            //
            // Validate
            //

            var form =
                new PersonFluffyDateModelState(
                    ModelState,
                    new FluffyDate(
                        editModel.Year,
                        editModel.Month,
                        editModel.Day),
                    editModel.Type,
                    person.FluffyDates,
                    fluffyDate.PersonFluffyDateId);

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

            form.HasUniqueTypeToBeEdited();
            if (!ModelState.IsValid)
            {
                return(View(editModel));
            }

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

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

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

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

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

            //
            // Edit
            //

            fluffyDate.Year  = editModel.Year;
            fluffyDate.Month = editModel.Month;
            fluffyDate.Day   = editModel.Day;
            fluffyDate.Type  = (PersonFluffyDateType)editModel.Type;

            try
            {
                _personData.UpdatePersonFluffyDate(fluffyDate);
            }
            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(editModel));
            }

            //
            // Update RDF
            //

            var readPerson =
                _personData.ReadAllPersonData(fluffyDate.Person.PersonGuid);

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

            //
            // Update search index
            //

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

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

            // Update age

            // Get updated dates
            person = _personData.ReadPersonWithFluffyDates(person.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 = fluffyDate.Person.PersonGuid }));
        }