コード例 #1
0
        public IActionResult Create(
            Guid personGuid,
            PersonDescriptionCreateModel createModel)
        {
            // Must have an GUID
            if (personGuid == Guid.Empty)
            {
                return(NotFound());
            }

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

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

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

            //
            // Validate
            //

            var form =
                new PersonDescriptionModelState(
                    ModelState,
                    new Logic.Validate.PersonDescription(
                        createModel.Text),
                    createModel.Type,
                    person.Descriptions,
                    null);

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

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

            //
            // Create
            //

            var newDescription = new PersonDescription();

            newDescription.PersonId    = person.PersonId;
            newDescription.Type        = (PersonDescriptionType)createModel.Type;
            newDescription.Description = createModel.Text;

            try
            {
                _personData.AddPersonDescription(newDescription);
            }
            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
            //

            // Descriptions are at this time not searchable.

            //
            // Redirect
            //

            return(RedirectToAction(
                       "Details",
                       "Person",
                       new { personGuid = person.PersonGuid }));
        }
コード例 #2
0
        public IActionResult Create(
            PersonCreateModel createModel)
        {
            //
            // Validate
            //

            // Abort if something is wrong
            if (!ModelState.IsValid)
            {
                return(View(createModel));
            }

            //
            // Create
            //

            var newPerson = new Person();

            newPerson.Gender = (PersonGender)createModel.Gender;

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

                return(View(createModel));
            }

            //
            // Update RDF
            //

            _rdfData.AddOrUpdatePerson(newPerson);

            //
            // Update search index
            //

            var persons = new List <PersonDocumentCreateModel>
            {
                // When creating a person we only have
                // PersonGuid and Gender.

                new PersonDocumentCreateModel
                {
                    PersonGuid = newPerson.PersonGuid.ToString(),
                    Gender     = newPerson.Gender.ToString()
                }
            };

            // TODO try catch to handle errors
            _personSearchIndex.UploadDocuments(persons);

            //
            // Redirect
            //

            return(RedirectToAction(
                       "Details",
                       "Person",
                       new { personGuid = newPerson.PersonGuid }));
        }
コード例 #3
0
		public IActionResult Create(
			Guid personGuid,
			PersonNameCreateModel createModel)
		{
			// Must have an GUID
			if (personGuid == Guid.Empty)
				return NotFound();

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

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

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

			//
			// Validate
			//

			var form =
				new PersonNameModelState(
					ModelState,
					new Logic.Validate.PersonName(
						createModel.Prefix,
						createModel.First,
						createModel.Middle,
						createModel.Last,
						createModel.Suffix,
						null), // no name weight value in creation form
					person.Names,
					null);

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

			form.HasUniquePersonNameToBeCreated();
			if (!ModelState.IsValid) return View(createModel);

			//
			// Create
			//

			var newName = new PersonName();
				newName.PersonId = person.PersonId;
				newName.Prefix = createModel.Prefix;
				newName.First = createModel.First;
				newName.Middle = createModel.Middle;
				newName.Last = createModel.Last;
				newName.Suffix = createModel.Suffix;

			// TimeCreated and TimeLastUpdated is set at class creation.

			// New name is added at the bottom of the list.
			newName.NameWeight = person.Names.Count + 1;

			try
			{
				_personData.AddPersonName(newName);
			}
			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
			//

			var names = new List<string>();

			person = _personData.ReadPersonWithNames(personGuid);

			foreach (var name in person.Names.OrderByDescending(x => x.NameWeight))
			{
				var fullName =
					string.Join(" ", new[]
					{
						name.Prefix,
						name.First,
						name.Middle,
						name.Last,
						name.Suffix
					}.Where(x => !string.IsNullOrWhiteSpace(x)).ToArray());

				names.Add(fullName);
			}

			// TODO try catch to handle errors
			_personSearchIndex.MergeNames(
				person.PersonGuid.ToString(),
				names.ToArray());

			//
			// Redirect
			//

			return RedirectToAction(
				"Details",
				"Person",
				new { personGuid = personGuid });
		}
コード例 #4
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 }));
        }