Exemplo n.º 1
0
 public IActionResult Create([FromBody] NewAddressDTO model)
 {
     try
     {
         var address = this.addressService.Create(model);
         return(Created("post", address));
     }
     catch (Exception e)
     {
         return(BadRequest(e.Message));
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Create an address.
        /// </summary>
        /// <param name="model">Details of the address to be created.</param>
        /// <returns>Returns created address or an appropriate error message.</returns>
        public AddressDTO Create(NewAddressDTO model)
        {
            var city = this.dbcontext.Cities.FirstOrDefault(c => c.Id == model.CityId)
                       ?? throw new ArgumentException(Exceptions.InvalidCity);
            var address = new Address();

            address.StreetName = model.StreetName;
            address.CityID     = model.CityId;
            address.CreatedOn  = DateTime.UtcNow;
            city.Addresses.Add(address);
            this.dbcontext.Addresses.Add(address);
            this.dbcontext.SaveChanges();
            return(new AddressDTO(address));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Update an address by a certain ID and data.
        /// </summary>
        /// <param name="id">ID of the address to update.</param>
        /// <param name="model">Details of the address to be updated.</param>
        /// <returns>Returns the updated address or an appropriate error message. </returns>
        public AddressDTO Update(int id, NewAddressDTO model)
        {
            var address = FindAddress(id);

            address.StreetName = model.StreetName ?? address.StreetName;
            if (model.CityId != 0)
            {
                var city = this.dbcontext
                           .Cities
                           .FirstOrDefault(c => c.Id == model.CityId)
                           ?? throw new ArgumentException(Exceptions.InvalidCity);
                address.CityID = model.CityId;
            }
            var warehouse = this.dbcontext.Warehouses.Include(w => w.Address)
                            .FirstOrDefault(w => w.Id == model.WarehouseId)
                            ?? throw new ArgumentException(Exceptions.InvalidWarehouse);

            address.Warehouse  = warehouse;
            address.ModifiedOn = DateTime.UtcNow;
            this.dbcontext.SaveChanges();
            return(new AddressDTO(address));
        }
Exemplo n.º 4
0
 public IActionResult Update([FromHeader] string authorizationUsername, int id, [FromBody] NewAddressDTO model)
 {
     try
     {
         this.authHelper.TryGetEmployee(authorizationUsername);
         var address = this.addressService.Update(id, model);
         return(Ok(address));
     }
     catch (Exception e)
     {
         return(NotFound(e.Message));
     }
 }