예제 #1
0
        public async Task <ShopDTO> UpdateAsync(ShopPutRequest model, ApiDbContext apiDbContext)
        {
            try
            {
                var shop = await apiDbContext.Shops.FindAsync(model.Id);

                if (shop == null)
                {
                    throw new Exception($"No existe la tienda {model.Name} con id {model.Id}");
                }

                var shopFoundEmail = apiDbContext.Shops.FirstOrDefault(u => u.Id != model.Id && u.Email.ToUpper().Trim().Equals(model.Email.ToUpper().Trim()));
                if (shopFoundEmail != null)
                {
                    throw new Exception($"Ya existe otra tienda con el Email {model.Email}");
                }

                var shopFoundName = apiDbContext.Shops.FirstOrDefault(u => u.Id != model.Id && u.Name.ToUpper().Trim().Equals(model.Name.ToUpper().Trim()));
                if (shopFoundName != null)
                {
                    throw new Exception($"Ya existe otra tienda {model.Name}");
                }

                var shopFoundCode = apiDbContext.Shops.FirstOrDefault(u => u.Id != model.Id && u.Name.ToUpper().Trim().Equals(model.Code.ToUpper().Trim()));
                if (shopFoundCode != null)
                {
                    throw new Exception($"Ya existe otra tienda con el código {model.Code}");
                }

                shop.IsActive       = model.IsActive;
                shop.Code           = model.Code;
                shop.Name           = model.Name;
                shop.Phone          = model.Phone;
                shop.Email          = model.Email;
                shop.Taxes          = model.Taxes;
                shop.MinAmountTaxes = model.MinAmountTaxes;
                shop.Address        = model.Address;
                shop.City           = model.City;
                shop.Web            = model.Web;
                shop.OwnerId        = model.OwnerId;

                await apiDbContext.SaveChangesAsync();

                return(await ModelToDTOAsync(shop, apiDbContext));
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
예제 #2
0
        public async Task <IActionResult> Update(ShopPutRequest model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("Petición de actualización inválida");
                }

                return(Ok(await _shopService.UpdateAsync(model, _apiDbContext)));
            }
            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }
        }