コード例 #1
0
        public async Task <IActionResult> CreateAddress([FromBody] AddressForCreationDto address)
        {
            try
            {
                if (address == null)
                {
                    _logger.LogError("Address received is a Null Object.");
                    return(BadRequest("Address object is null. Please send full request."));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid Address object sent from client.");
                    return(BadRequest("Address object is not Valid"));
                }

                var addressEntity = _mapper.Map <Address>(address);

                _repositoryWrapper.Address.CreateAddress(addressEntity);
                await _repositoryWrapper.Save();

                var createdProperty = _mapper.Map <AddressDto>(addressEntity);
                return(CreatedAtRoute("AddressById", new { id = createdProperty.Id }, createdProperty));
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside CreateAddress(addressDto) action: {e.Message}");
                return(StatusCode(500, e.Message));
            }
        }
コード例 #2
0
        public IActionResult CreateAddress(Guid organizationId, [FromBody] AddressForCreationDto address)
        {
            if (organizationId == new Guid())
            {
                return(BadRequest());
            }

            if (address == null)
            {
                return(BadRequest());
            }

            if (!_unitOfWork.Addresses.IsOrganizationExists(organizationId))
            {
                return(NotFound());
            }

            var mappedAddress = _unitOfWork.Addresses.CreateAddress(organizationId, address);

            if (!_unitOfWork.Complete())
            {
                return(StatusCode(500, "A problem happened while handling your request!"));
            }

            var addressToReturn = _mapper.Map <AddressDto>(mappedAddress);


            return(CreatedAtRoute("GetAddressForOrganization",
                                  new { organizationId = organizationId, addressId = addressToReturn.AddressId }, CreateLinksForAddress(addressToReturn)));
        }
コード例 #3
0
        public IActionResult CreateAddress([FromBody] AddressForCreationDto addressDto)
        {
            var createAddress = _commandFactory.MakeCommand <CreateAddressCommand>();

            createAddress.Execute(addressDto);

            return(Ok());
        }
コード例 #4
0
        public async Task <ActionResult> Post([FromBody] AddressForCreationDto addressForCreation)
        {
            var address = _mapper.Map <Address>(addressForCreation);

            _ = await _addressService.Insert(address);

            return(Response(address.AddressId));
        }
コード例 #5
0
        public Address CreateAddress(Guid organizationId, AddressForCreationDto address)
        {
            var organizationFromContext =
                _context.Organizations.FirstOrDefault(o => o.OrganizationId == organizationId);
            var mappedAddress = _mapper.Map <Address>(address);

            organizationFromContext.Addresses.Add(mappedAddress);

            return(mappedAddress);
        }
コード例 #6
0
        public async Task <IActionResult> Create([FromBody] AddressForCreationDto addressForCreationDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Object is invalid."));
            }

            var address = new Address()
            {
                CountryId = addressForCreationDto.CountryId,
                City      = addressForCreationDto.City,
                ZipCode   = addressForCreationDto.ZipCode,
                Street    = addressForCreationDto.Street
            };

            this._repository.Address.Create(address);
            await this._repository.SaveAsync();

            return(Ok());
        }
コード例 #7
0
        public IActionResult CreateAddress([FromBody] AddressForCreationDto address)
        {
            try
            {
                if (address == null)
                {
                    _logger.LogError("Address object sent from client is null.");
                    return(BadRequest("Address object is null"));
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid address object sent from client.");
                    return(BadRequest("Invalid model object"));
                }

                if (IsAddressAdded(null, address.Title, address.PersonId))
                {
                    _logger.LogError($"Address with title: {address.Title} added before to person Id: {address.PersonId}.");
                    return(BadRequest("Address added before"));
                }

                var addressEntity = _mapper.Map <Address>(address);

                _repository.Address.CreateAddress(addressEntity);
                _repository.Save();

                var createdAddress = _mapper.Map <AddressDto>(addressEntity);

                return(CreatedAtRoute("GetAddressById", new { id = createdAddress.Id }, createdAddress));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside CreateAddress action: {ex.Message}");
                if (ex.InnerException != null)
                {
                    _logger.LogError($"Inner Exception: {ex.InnerException.Message}");
                }
                return(StatusCode(500, "Internal server error"));
            }
        }
コード例 #8
0
        public async Task <IActionResult> Post([FromBody] AddressForCreationDto addressForCreationDto)
        {
            if (await this._customerRepo.GetSingle(addressForCreationDto.CustomerId) == null)
            {
                return(BadRequest());
            }

            var address = new Address
            {
                AddressLine1 = addressForCreationDto.AddressLine1,
                AddressLine2 = addressForCreationDto.AddressLine2,
                Street       = addressForCreationDto.Street,
                Town         = addressForCreationDto.Town,
                Postcode     = addressForCreationDto.Postcode,
                CustomerId   = addressForCreationDto.CustomerId
            };

            await _addressRepo.Create(address);

            return(Ok(addressForCreationDto));
        }