Пример #1
0
        public async Task <IActionResult> UpdateUserAndPersonAsync(int id, [FromBody] Person person)
        {
            if (person == null)
            {
                return(BadRequest());
            }

            var oldPerson = await _personRepository.GetFilterByInheritedAsync(typeof(User), id);

            var oldUser = await _userRepository.GetAsync(id);

            if (oldPerson == null || oldUser == null)
            {
                return(NotFound());
            }

            person.Id = id;

            if (!await _personRepository.UpdateAsync(person))
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to update users personal details"));
            }


            //comment:
            // User currently does not contain any field -> No need to update.
            // If update of User is needed, replace the Person object in '[FromBody] Person' with a DTO with both that implements IPerson and IUser and use Automapper to create Person and User objects.
            // Then find differences between old and new object on both person and object (using _changeLog.Changes(oldObj, updatedObj) and save to database

            _log.LogUpated(HttpContext.Request.Method, HttpContext.Request.Path, oldPerson, person);

            return(NoContent());
        }
Пример #2
0
        public async Task <IActionResult> DeleteUserAndPersonAsync(int id)
        {
            var person = await _personRepository.GetAsync(id);

            var user = await _userRepository.GetAsync(id);

            if (user == null || person == null)
            {
                return(NotFound());
            }

            if (!await _userRepository.DeleteAsync(user))
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to delete user"));
            }

            if (!await _personRepository.DeleteAsync(person))
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to delete person"));
            }

            _log.LogDeleted(HttpContext.Request.Method, HttpContext.Request.Path, multipleObjectsToJson(user, person).ToString());

            return(NoContent());
        }
Пример #3
0
        public async Task <IActionResult> GetPersonAsync(int id)
        {
            var person = await _personRepository.GetAsync(id);

            if (person == null)
            {
                return(NotFound());
            }

            return(Ok(person));
        }
Пример #4
0
        public async Task <IActionResult> GetAsync(int personId)
        {
            if (await _personRepository.GetAsync(personId) == null)
            {
                return(NotFound());
            }

            var addresses = await _addressRepository.GetFilterByForeignKey(personId, nameof(Address.PersonId));

            if (addresses == null)
            {
                return(NotFound());
            }

            return(Ok(addresses));
        }
Пример #5
0
        public async Task <IActionResult> GetAsync(int personId)
        {
            if (await _personRepository.GetAsync(personId) == null)
            {
                return(NotFound());
            }

            var emails = await _emailRepository.GetFilterByForeignKey(personId, nameof(Email.PersonId));

            if (emails == null)
            {
                return(NotFound());
            }

            return(Ok(emails));
        }
Пример #6
0
        public async Task <IActionResult> DeleteAsync(int id)
        {
            var person = await _personRepository.GetAsync(id);

            if (person == null)
            {
                return(NotFound());
            }

            var user = await _userRepository.GetAsync(id);

            if (user != null)
            {
                await _userRepository.DeleteAsync(user);

                _log.LogDeleted(HttpContext.Request.Method, HttpContext.Request.Path, "user: "******"email: " + JsonConvert.SerializeObject(email).ToString());
            }

            var addresses = await _addressRepository.GetFilterByForeignKey(id, nameof(Address.PersonId));

            foreach (var address in addresses)
            {
                await _addressRepository.DeleteAsync(address);

                _log.LogDeleted(HttpContext.Request.Method, HttpContext.Request.Path, "address: " + JsonConvert.SerializeObject(address).ToString());
            }

            if (!await _personRepository.DeleteAsync(person))
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, "Unable to delete person"));
            }

            _log.LogDeleted(HttpContext.Request.Method, HttpContext.Request.Path, JsonConvert.SerializeObject(person).ToString());

            return(NoContent());
        }