Esempio n. 1
0
        public async Task <IActionResult> Create(ShopPostRequest model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("Petición de alta inválida");
                }

                return(Ok(await _shopService.CreateAsync(model, _apiDbContext)));
            }
            catch (Exception e)
            {
                return(StatusCode(500, e.Message));
            }
        }
Esempio n. 2
0
        public async Task <ShopDTO> CreateAsync(ShopPostRequest model, ApiDbContext apiDbContext)
        {
            try
            {
                var shopFound = apiDbContext.Shops.FirstOrDefault(s => s.Name.ToUpper().Trim().Equals(model.Name.ToUpper().Trim()));
                if (shopFound != null)
                {
                    throw new Exception($"Ya existe una tienda con el nombre {model.Email}");
                }

                shopFound = apiDbContext.Shops.FirstOrDefault(s => s.Code.ToUpper().Trim().Equals(model.Code.ToUpper().Trim()));
                if (shopFound != null)
                {
                    throw new Exception($"Ya existe una tienda con el código {model.Code}");
                }

                Shop shop = new Shop
                {
                    Nif            = model.Nif,
                    IsActive       = model.IsActive,
                    Code           = model.Code,
                    Name           = model.Name,
                    Phone          = model.Phone,
                    Email          = model.Email,
                    Taxes          = model.Taxes,
                    MinAmountTaxes = model.MinAmountTaxes,
                    Address        = model.Address,
                    City           = model.City,
                    Picture        = model.Picture,
                    Web            = model.Web,
                    CreationDate   = DateTime.Now,
                    OwnerId        = model.OwnerId
                };

                await apiDbContext.Shops.AddAsync(shop);

                await apiDbContext.SaveChangesAsync();

                return(await ModelToDTOAsync(shop, apiDbContext));
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }