Exemple #1
0
        public Person Save(MPerson person)
        {
            var institutionId = 0;

            if (!string.IsNullOrEmpty(person.Institution?.Name))
            {
                institutionId = _uInstitution.GetExact(person.Institution.Name)?.Id ?? 0;
                if (institutionId == 0)
                {
                    institutionId = _bInstitution.Save(person.Institution.Name).Id;
                }
            }
            var cPerson = new Person
            {
                Id        = person.Id,
                Name      = person.Name,
                Cpf       = person.Cpf.RCpf(),
                Email     = person.Email,
                BirthDate = person.BirthDate,
                Sex       = person.Sex,
                Password  = person.Password.Sha256Hash(),
                Profile   = person.Profile
            };

            if (institutionId != 0)
            {
                cPerson.InstitutionId = institutionId;
            }

            Save(cPerson);
            _context.SaveChanges();
            return(cPerson);
        }
Exemple #2
0
        public IActionResult Update([FromBody] MPersonUpdate mPerson)
        {
            if (!HttpContext.Session.IsAuth())
            {
                return(StatusCode((int)HttpStatusCode.Unauthorized, ResponseContent.Create(null, HttpStatusCode.Unauthorized, "Não autorizado!")));
            }

            if (string.IsNullOrEmpty(mPerson.Name))
            {
                return(BadRequest(ResponseContent.Create(null, HttpStatusCode.BadRequest, "Seu nome não pode estar em vazio!")));
            }

            if (!string.IsNullOrEmpty(mPerson.Password) && !mPerson.Password.IsValidPassord())
            {
                return(BadRequest(ResponseContent.Create(null, HttpStatusCode.BadRequest, "A senha deve conter ao menos 6 caracteres, dentre eles, uma letra e um número!")));
            }

            var person = _uPerson.GetById(HttpContext.Session.GetSessionPersonId());

            if (!string.IsNullOrEmpty(mPerson.Password))
            {
                person.Password = mPerson.Password.Sha256Hash();
            }

            if (mPerson.Institution != null && mPerson.Institution.Name != person.Institution?.Name && !string.IsNullOrEmpty(mPerson.Institution.Name))
            {
                person.Institution = _uInstitution.GetExact(mPerson.Institution.Name) ?? _bInstitution.Save(mPerson.Institution.Name);
            }
            else if (string.IsNullOrEmpty(mPerson.Institution?.Name) && !string.IsNullOrEmpty(person.Institution?.Name))
            {
                person.Institution = null;
            }

            if (!string.IsNullOrEmpty(mPerson.Photo))
            {
                var file = FileExtensions.CopyFromTo(new List <string> {
                    mPerson.Photo
                }, _configuration["Directory:TEMP"], _configuration["Directory:UPLOAD"]).FirstOrDefault();
                person.PhotoPath = file;
            }

            person.Name = mPerson.Name;

            _bPerson.Update(person);
            _bHistory.SaveHistory(person.Id, "Usuário editou seu cadastro");

            HttpContext.Session.Authenticate(person);
            return(Ok(ResponseContent.Create(null, HttpStatusCode.OK, "Usuário editado com sucesso!")));
        }