public async Task <IActionResult> UpdateAsync( [FromBody] UserBusinessViewModel user) { try { var login = Util.GetClaim(_httpContext, Constants.UserClaimIdentifier); var userMap = _mapper.Map <User>(user); var userDb = await _userMananger .GetByLogin(login); // usuario não existe if (userDb == null) { return(NotFound(new ResponseErrorViewModel { Status = Constants.Error, Errors = new List <object> { new { Message = Constants.UserNotFound } } })); } // usaurio não foi verificado if (!userDb.IsVerified()) { return(BadRequest( new ResponseErrorViewModel { Status = Constants.Error, Errors = new List <object> { new { Message = Constants.UserNotValidate } } } )); } if (!string.IsNullOrEmpty(userMap.Person.Name)) { userDb.Person.Name = userMap.Person.Name; } if (!string.IsNullOrEmpty(userMap.Person.LastName)) { userDb.Person.LastName = userMap.Person.LastName; } userDb.Person.Description = userMap.Person.Description; userDb.Person.UrlImage = userMap.Person.UrlImage; userDb.Person.WebSite = userMap.Person.WebSite; userDb.Person.UpdatedAt = Util.CurrentDateTime(); // phones if (userMap.Person.Phones != null && userMap.Person.Phones.Count > 0) { foreach (var phone in userMap.Person.Phones) { var numberFormated = phone.Number .Replace("+", "") .Replace("(", "") .Replace(")", "") .Replace("-", "") .Replace(" ", "") .Trim(); if (!string.IsNullOrEmpty(numberFormated)) { var phoneDb = (userDb.Person.Phones != null && userDb.Person.Phones.Count > 0) ? userDb.Person.Phones .SingleOrDefault(x => x.Master == true) : null; if (phoneDb == null) { userDb.Person.Phones.Add(new Phone { Number = numberFormated, Master = true, Person = userDb.Person, IdPerson = userDb.Person.Id, Active = true, CreatedAt = Util.CurrentDateTime(), UpdatedAt = Util.CurrentDateTime() }); } else { if (!string.IsNullOrEmpty(numberFormated)) { phoneDb.Number = numberFormated; } phoneDb.Active = true; phoneDb.UpdatedAt = Util.CurrentDateTime(); } } } } // addresses if (userMap.Person.Addresses != null && userMap.Person.Addresses.Count > 0) { foreach (var address in userMap.Person.Addresses) { var addressDb = userDb.Person.Addresses .SingleOrDefault(); if (addressDb == null) { var addressResponse = await Util .GetAddressByLatitudeLogintude(_configuration, address.Latitude, address.Longitude); userDb.Person.Addresses.Add(new Address { Longitude = address.Longitude, Latitude = address.Latitude, FullAddress = addressResponse.FullAddress, City = addressResponse.City, Street = address.Street, State = address.State, Complement = address.Complement, Neighborhood = address.Neighborhood, Number = address.Number, ZipCode = address.ZipCode .Trim() .Replace("-", "") .Replace(" ", ""), Active = true, IdPerson = userDb.Person.Id, Person = userDb.Person, Master = true, NickName = "Principal", CreatedAt = Util.CurrentDateTime(), UpdatedAt = Util.CurrentDateTime(), }); } else { if (!string.IsNullOrEmpty(address.Latitude) && !string.IsNullOrEmpty(address.Longitude)) { var addressResponse = await Util .GetAddressByLatitudeLogintude(_configuration, address.Latitude, address.Longitude); addressDb.Latitude = address.Latitude; addressDb.Longitude = address.Longitude; addressDb.City = addressResponse.City; addressDb.Street = address.Street; addressDb.State = address.State; addressDb.Complement = address.Complement; addressDb.Neighborhood = address.Neighborhood; addressDb.Number = address.Number; addressDb.ZipCode = address.ZipCode.Trim() .Replace("-", "") .Replace(" ", ""); addressDb.FullAddress = addressResponse.FullAddress; addressDb.Active = true; addressDb.UpdatedAt = Util.CurrentDateTime(); } addressDb.UpdatedAt = Util.CurrentDateTime(); } } } _userMananger.Update(userDb); _uow.Commit(); userDb = await _userMananger .GetByIdAsync(userDb.IdPerson); user = _mapper.Map <UserBusinessViewModel>(userDb); return(Ok(new ResponseViewModel { Result = user, Status = Constants.Sucess })); } catch (Exception ex) { // gerar log return(BadRequest(new ResponseErrorViewModel { Status = Constants.Error, Errors = new List <object> { Util.ReturnException(ex) } })); } }
public async Task <IActionResult> RegisterBusinessAsync( [FromBody] UserBusinessViewModel user ) { try { var userMap = _mapper.Map <User>(user); // verificar se campos estão preenchidos if (string.IsNullOrEmpty(userMap.Person.Document)) { return(BadRequest(new ResponseErrorViewModel { Result = Constants.Error, Errors = new List <object> { new { Message = Constants.UserDocumentInvalid } } })); } if (string.IsNullOrEmpty(userMap.Login)) { return(BadRequest(new ResponseErrorViewModel { Result = Constants.Error, Errors = new List <object> { new { Message = Constants.UserLoginInvalid } } })); } var document = userMap.Person.Document .Replace(".", "") .Replace("/", "") .Replace("-", "") .Trim(); if (!Util.IsValidCpf(document)) { return(BadRequest(new ResponseErrorViewModel { Result = Constants.Error, Errors = new List <object> { new { Message = Constants.UserCnpjInvalid } } })); } var userDb = await _userMananger .GetByLoginOrDocumentNoTracking(userMap.Login, document); // usuario já existe if (userDb != null) { return(Conflict( new ResponseErrorViewModel { Status = Constants.Error, Errors = new List <object> { new { Message = Constants.UserFound } } })); } // captura perfil de usuario padrão var profileDb = await _profileMananger .GetByIdAsync(1); var newPhone = userMap.Person.Phones .FirstOrDefault(x => x.Number.Equals(user.Phone)); newPhone.Number = newPhone.Number .Replace("(", "") .Replace(")", "") .Replace("-", "") .Replace(" ", ""); newPhone.Active = true; newPhone.Master = true; newPhone.CreatedAt = Util.CurrentDateTime(); newPhone.UpdatedAt = newPhone.CreatedAt; // remover caracteres especiais do cep if (userMap.Person.Addresses != null && userMap.Person.Addresses.Count > 0) { var address = userMap.Person.Addresses.SingleOrDefault(); address.FullAddress = string.Concat(address.Street, ", ", address.Neighborhood, ", ", address.City, " - ", address.State, ", ", address.ZipCode); address.ZipCode = address.ZipCode .Trim().Replace("-", ""); address.Master = true; address.Active = true; address.NickName = "Principal"; address.CreatedAt = Util.CurrentDateTime(); address.UpdatedAt = address.CreatedAt; } userMap.Person.Document = document; userMap.LoginVerified = false; userMap.Profile = profileDb; userMap.Password = Util.Encrypt(userMap.Password); userMap.Person.Active = true; userMap.Person.PersonType = "PJ"; userMap.Person.CreatedAt = Util.CurrentDateTime(); userMap.Person.UpdatedAt = userMap.Person.CreatedAt; await _userMananger.CreateAsync(userMap); // create library var lib = new Library { Person = userMap.Person, Active = true, CreatedAt = Util.CurrentDateTime(), UpdatedAt = Util.CurrentDateTime() }; await _libraryMananger.CreateAsync(lib); user = _mapper.Map <UserBusinessViewModel>(userMap); var emailVerification = new EmailVerification { Login = userMap.Login, CreatedAt = Util.CurrentDateTime(), CodeVerification = Util.GenerateGuid() }; await _emailVerificationService.CreateAsync(emailVerification); Util.SendEmailAsync(_configuration, emailVerification.CodeVerification, user.Login, user.Name, HtmlEmailType.NewAccount, HtmlEmailPersonType.LegalPerson); _uow.Commit(); return(Created(new Uri(Url .ActionLink("RegisterBusiness", "Account")), new ResponseViewModel { Result = null, Status = Constants.Sucess })); } catch (Exception ex) { // gerar log return(BadRequest(new ResponseErrorViewModel { Status = Constants.Error, Errors = new List <object> { Util.ReturnException(ex) } })); } }