/// <summary> /// Responsable for address validation. /// </summary> /// <param name="contact">The contact.</param> private static void AddressValidation(ContactVWM contact) { if (string.IsNullOrEmpty(contact.ZipCode)) { throw new Exception("The field Zip Code is required."); } if (string.IsNullOrEmpty(contact.Country)) { throw new Exception("The field country is required."); } if (string.IsNullOrEmpty(contact.State)) { throw new Exception("The field state is required."); } if (string.IsNullOrEmpty(contact.City)) { throw new Exception("The field city is required."); } if (string.IsNullOrEmpty(contact.AddressLine1) && string.IsNullOrEmpty(contact.AddressLine2)) { throw new Exception("You must provide at least one address."); } }
public IActionResult AddOrEdit(string id) { try { PrepareViewBags(); if (string.IsNullOrEmpty(id)) { return(View(new ContactVWM())); } else { var dataBaseEntity = _contactService.GetContact(id); ContactVWM contactVwm = ModelToVwm(dataBaseEntity); return(View(contactVwm)); } } catch (Exception erro) { return(Json(new ValidateMessage { Message = erro.Message, IsError = true })); } }
/// <summary> /// Responsible to convert Models to VWM. /// </summary> /// <param name="dataBaseEntity">The data base entity.</param> /// <returns>Return the entity vwm.</returns> private static ContactVWM ModelToVwm(Contact dataBaseEntity) { var contactVwm = new ContactVWM { Id = dataBaseEntity.Id }; contactVwm.Type = dataBaseEntity.Person.Type; if (dataBaseEntity.Person.Type == EnumTypePerson.NATURAL) { contactVwm.Name = dataBaseEntity.Person.NaturalPerson.Name; contactVwm.Birthday = dataBaseEntity.Person.NaturalPerson.Birthday; contactVwm.Gender = dataBaseEntity.Person.NaturalPerson.Gender; contactVwm.Cpf = dataBaseEntity.Person.NaturalPerson.Cpf; } else { contactVwm.CompanyName = dataBaseEntity.Person.LegalPerson.CompanyName; contactVwm.TradeName = dataBaseEntity.Person.LegalPerson.TradeName; contactVwm.Cnpj = dataBaseEntity.Person.LegalPerson.Cnpj; } contactVwm.ZipCode = dataBaseEntity.Person.Address.ZipCode; contactVwm.Country = dataBaseEntity.Person.Address.Country; contactVwm.State = dataBaseEntity.Person.Address.State; contactVwm.City = dataBaseEntity.Person.Address.City; contactVwm.AddressLine1 = dataBaseEntity.Person.Address.AddressLine1; contactVwm.AddressLine2 = dataBaseEntity.Person.Address.AddressLine2; return(contactVwm); }
public IActionResult AddOrEdit(ContactVWM contact) { try { if (string.IsNullOrEmpty(contact.Id)) { ContactValidator validator = new ContactValidator(_contactService); validator.InsertValidator(contact); Contact newContact = PrepareNewContact(contact); _contactService.Insert(newContact); } else { ContactValidator validator = new ContactValidator(_contactService); validator.UpdateValidator(contact); var dataBaseEntity = _contactService.GetContact(contact.Id); PrepareUpdateContact(contact, dataBaseEntity); _contactService.Update(dataBaseEntity); } return(Json(true)); } catch (Exception erro) { return(Json(new ValidateMessage { Message = erro.Message, IsError = true })); } }
/// <summary> /// Prepares the new contact. /// </summary> /// <param name="contact">The contact.</param> /// <returns>Return the new contact.</returns> private Contact PrepareNewContact(ContactVWM contact) { Contact newContact = new Contact { Id = _contactService.GetNextId() }; var person = new Person { Id = _contactService.GetNextId(), Type = contact.Type, Address = new Address { Id = _contactService.GetNextId(), ZipCode = contact.ZipCode, City = contact.City, State = contact.State, AddressLine1 = contact.AddressLine1, AddressLine2 = contact.AddressLine2, Country = contact.Country, } }; newContact.Person = person; if (contact.Type == EnumTypePerson.NATURAL) { newContact.Person.NaturalPerson = new NaturalPerson { Id = _contactService.GetNextId(), Birthday = contact.Birthday.Value, Cpf = contact.Cpf, Gender = contact.Gender.Value, Name = contact.Name }; } else { newContact.Person.LegalPerson = new LegalPerson { Id = _contactService.GetNextId(), Cnpj = contact.Cnpj, CompanyName = contact.CompanyName, TradeName = contact.TradeName }; } return(newContact); }
/// <summary> /// Prepares the update contact. /// </summary> /// <param name="contact">The contact.</param> /// <param name="dataBaseEntity">The data base entity.</param> private void PrepareUpdateContact(ContactVWM contact, Contact dataBaseEntity) { dataBaseEntity.Person.Type = contact.Type; dataBaseEntity.Person.Address.ZipCode = contact.ZipCode; dataBaseEntity.Person.Address.City = contact.City; dataBaseEntity.Person.Address.State = contact.State; dataBaseEntity.Person.Address.Country = contact.Country; dataBaseEntity.Person.Address.AddressLine1 = contact.AddressLine1; dataBaseEntity.Person.Address.AddressLine2 = contact.AddressLine2; if (contact.Type == EnumTypePerson.NATURAL) { if (dataBaseEntity.Person.NaturalPerson == null) { dataBaseEntity.Person.NaturalPerson = new NaturalPerson { Id = _contactService.GetNextId(), }; dataBaseEntity.Person.LegalPerson = null; } dataBaseEntity.Person.NaturalPerson.Birthday = contact.Birthday.Value; dataBaseEntity.Person.NaturalPerson.Cpf = contact.Cpf; dataBaseEntity.Person.NaturalPerson.Gender = contact.Gender.Value; dataBaseEntity.Person.NaturalPerson.Name = contact.Name; } else { if (dataBaseEntity.Person.LegalPerson == null) { dataBaseEntity.Person.LegalPerson = new LegalPerson { Id = _contactService.GetNextId(), }; dataBaseEntity.Person.NaturalPerson = null; } dataBaseEntity.Person.LegalPerson.Cnpj = contact.Cnpj; dataBaseEntity.Person.LegalPerson.CompanyName = contact.CompanyName; dataBaseEntity.Person.LegalPerson.TradeName = contact.TradeName; } }
/// <summary> /// Responsable for Generals validation. /// </summary> /// <param name="contact">The contact.</param> private void GeneralValidation(ContactVWM contact) { if (contact == null) { throw new Exception("Incorret object."); } if (contact.Type == Domain.Enumerator.EnumTypePerson.NATURAL) { if (string.IsNullOrEmpty(contact.Name)) { throw new Exception("The field name is required."); } if (!contact.Gender.HasValue) { throw new Exception("The field gender is required."); } if (string.IsNullOrEmpty(contact.Cpf)) { throw new Exception("The field Cpf is required."); } else { if (!ValidationHelper.CpfValidatior(contact.Cpf.Replace(".", string.Empty).Replace("-", string.Empty))) { throw new Exception("The CPF informed is invalid."); } if (this._contactService.AmountPeopleSameCpf(contact.Cpf, contact.Id) > 0) { throw new Exception("There is already a person registered with this same cpf."); } } if (!contact.Birthday.HasValue) { throw new Exception("The field birthday is required."); } else { if (contact.Birthday.Value.Date >= DateTime.Now.Date) { throw new Exception("The birthday date must be less than today's date."); } } } else { if (string.IsNullOrEmpty(contact.CompanyName)) { throw new Exception("The field company name is required."); } if (string.IsNullOrEmpty(contact.TradeName)) { throw new Exception("The field trade name is required."); } if (string.IsNullOrEmpty(contact.Cnpj)) { throw new Exception("The field cnpj is required."); } else { if (!ValidationHelper.CnpjValidator(contact.Cnpj.Replace(".", string.Empty).Replace("-", string.Empty))) { throw new Exception("The CNPJ informed is invalid."); } if (this._contactService.AmountPeopleSameCnpj(contact.Cnpj, contact.Id) > 0) { throw new Exception("There is already a person registered with this same cnpj."); } } } AddressValidation(contact); }
/// <summary> /// Responsable for insert validation. /// </summary> /// <param name="contact">The contact.</param> public void InsertValidator(ContactVWM contact) { GeneralValidation(contact); }
/// <summary> /// Responsable for update validation. /// </summary> /// <param name="contact">The contact.</param> public void UpdateValidator(ContactVWM contact) { GeneralValidation(contact); }