Exemple #1
0
        public async Task <JsonResult> AddPeopleJSON(PeopleViewModel people)
        {
            try
            {
                var peopleDto = new PeopleDTO
                {
                    Id           = people.Id,
                    FirstName    = people.FirstName,
                    LastName     = people.LastName,
                    SecondName   = people.SecondName,
                    Email        = people.Email,
                    DateBirthday = people.DateBirthday
                };

                if (people.Id > 0)
                {
                    await peopleService.EditPeopleTask(peopleDto);
                }
                else
                {
                    var result = await peopleService.AddPeopleTask(peopleDto);

                    if (!result.Successed)
                    {
                        return(Json(new { success = false, Message = result.ErrorMessage }, JsonRequestBehavior.AllowGet));
                    }

                    people.Id = result.Id;
                }

                foreach (var phone in people.Phones)
                {
                    if (phone.ForDelete && phone.Id > 0)
                    {
                        await peopleService.DelPhoneForPeopleTask(phone.Id, people.Id);
                    }

                    if (phone.Id == 0 && !phone.ForDelete)
                    {
                        await peopleService.AddPhoneForPeopleTask(phone.PhoneNumber, people.Id);
                    }

                    if (!phone.ForDelete && phone.Id > 0)
                    {
                        await peopleService.EditPhoneForPeopleTask(phone.Id, people.Id, phone.PhoneNumber);
                    }
                }

                return(Json(new { success = true, data = people }, JsonRequestBehavior.AllowGet));
            }
            catch (ValidationException ex)
            {
                ModelState.AddModelError(ex.Property, ex.Message);
                return(Json(new { success = false, errorstring = ex.Message }, JsonRequestBehavior.AllowGet));
            }
        }