Example #1
0
        public async Task <IActionResult> Delete([FromRoute] string id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await _userManager.FindByIdAsync(id);

            if (user == null)
            {
                return(NotFound($"User with id {id} could not be found"));
            }

            var role = (await _userManager.GetRolesAsync(user)).First();

            var dtoToReturn = role.ToUpper() switch
            {
                "CUSTOMER" => (UserDto)await _customerRepository.Delete(user.Id),
                "DRIVER" => await _driverRepository.Delete(user.Id),
                "COMPANY" => await _companyRepository.Delete(user.Id),
                _ => throw new ArgumentOutOfRangeException()
            };

            dtoToReturn.Email       = user.Email;
            dtoToReturn.PhoneNumber = user.PhoneNumber;

            var addresses = await _addressRepository
                            .GetFiltered(a => a.UserId == user.Id)
                            .ToListAsync();

            addresses?.ForEach(a =>
            {
                _addressRepository.Delete(a.Id);
            });

            var result = await _userManager.DeleteAsync(user);

            if (!result.Succeeded)
            {
                return(BadRequest($"User with id {id} could not be deleted"));
            }

            return(Ok(dtoToReturn));
        }