コード例 #1
0
        public async Task <IActionResult> Edit(EditProduktVM model)
        {
            if (ModelState.IsValid)
            {
                Produkt product = _context.Produkt.FirstOrDefault(c => c.Id == model.Id);
                product.Ime        = model.Ime;
                product.NamenetoZa = model.NamenetoZa;
                product.Cena       = model.Cena;
                if (model.ProfilePicture != null)
                {
                    if (model.ExistingPhotoPath != null)
                    {
                        string filePath = Path.Combine(webHostEnvironment.WebRootPath,
                                                       "images", model.ExistingPhotoPath);
                        System.IO.File.Delete(filePath);
                    }

                    product.ProfilePicture = UploadedFile(model);
                }

                await _context.SaveChangesAsync();;

                return(RedirectToAction("index"));
            }

            return(View(model));
        }
コード例 #2
0
        public async Task <Guid> Handle(CreateProductCommand request, CancellationToken cancellationToken)
        {
            var newProduct = new Product
            {
                Name           = request.Name,
                Description    = request.Description,
                Price          = request.Price,
                IsAnimal       = request.IsAnimal,
                CreateDateUtc  = DateTime.UtcNow,
                DeletedDateUtc = null,
            };

            var doesProductExist =
                _context.Products.FirstOrDefault(x => x.Name.Equals(newProduct.Name)) != null;

            if (doesProductExist)
            {
                return(await Task.FromResult(Guid.Empty));
            }

            await _context.Products.AddAsync(newProduct, cancellationToken);

            var productValidator        = new ProductValidator();
            var productValidationResult = await productValidator.ValidateAsync(newProduct, cancellationToken);

            if (!productValidationResult.IsValid)
            {
                return(await Task.FromResult(Guid.Empty));
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(await Task.FromResult(newProduct.Id));
        }
コード例 #3
0
        public async Task <Guid> Handle(CreateCustomerCommand request, CancellationToken cancellationToken)
        {
            var newCustomer = new Customer
            {
                FirstName      = request.FirstName,
                LastName       = request.LastName,
                Address        = request.Address,
                Email          = request.Email,
                CreateDateUtc  = DateTime.UtcNow,
                DeletedDateUtc = null,
            };

            var doesCustomerExist = _context.Customers.FirstOrDefault(x =>
                                                                      x.FirstName.Equals(newCustomer.FirstName) &&
                                                                      x.LastName.Equals(newCustomer.LastName)) != null;

            if (doesCustomerExist)
            {
                return(await Task.FromResult(Guid.Empty));
            }

            await _context.Customers.AddAsync(newCustomer, cancellationToken);

            var customerValidator        = new CustomerValidator();
            var customerValidationResult = await customerValidator.ValidateAsync(newCustomer, cancellationToken);

            if (!customerValidationResult.IsValid)
            {
                return(await Task.FromResult(Guid.Empty));
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(await Task.FromResult(newCustomer.Id));
        }
コード例 #4
0
        public async Task <Customer> Handle(UpdateCustomerInfoCommand request, CancellationToken cancellationToken)
        {
            var customer = await _context.Customers.FindAsync(request.Id);

            if (customer is null)
            {
                return(await Task.FromResult <Customer>(null));
            }

            customer.FirstName = request.FirstName ?? customer.FirstName;
            customer.LastName  = request.LastName ?? customer.LastName;
            customer.Address   = request.Address ?? customer.Address;
            customer.Email     = request.Email ?? customer.Email;
            customer.Phone     = request.Phone ?? customer.Phone;

            var customerValidator = new CustomerValidator();
            var validationResult  = await customerValidator.ValidateAsync(customer, cancellationToken);

            if (!validationResult.IsValid)
            {
                return(await Task.FromResult <Customer>(null));
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(await Task.FromResult <Customer>(customer));
        }
コード例 #5
0
        public async Task <Product> Handle(UpdateProductInfoCommand request, CancellationToken cancellationToken)
        {
            var product = await _context.Products.FindAsync(request.Id);

            if (product is null)
            {
                return(await Task.FromResult <Product>(null));
            }

            product.Name        = request.Name ?? product.Name;
            product.Description = request.Description ?? product.Description;
            product.Price       = request.Price ?? product.Price;
            product.IsAnimal    = request.IsAnimal ?? product.IsAnimal;

            var productValidator = new ProductValidator();
            var validationResult = await productValidator.ValidateAsync(product, cancellationToken);

            if (!validationResult.IsValid)
            {
                return(await Task.FromResult <Product>(null));
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(await Task.FromResult(product));
        }
コード例 #6
0
        public async Task <IActionResult> CreateSopstvenik(SopstvenikVM model)
        {
            if (ModelState.IsValid)
            {
                string     uniqueFileName = UploadedFile(model);
                Sopstvenik sopstvenik     = new Sopstvenik
                {
                    ImePrezime     = model.ImePrezime,
                    ImeMilenik     = model.ImeMilenik,
                    Email          = model.Email,
                    Grad           = model.Grad,
                    ProfilePicture = uniqueFileName,
                };
                _context.Add(sopstvenik);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(IndexSopstvenik)));
            }
            return(View(model));
        }
コード例 #7
0
 public async Task AddOrEditAnimalAsync(Animal animal)
 {
     //Check if animal has got an Id, and if so update it.
     if (animal.AnimalId != 0)
     {
         //Check if animal exists loacally, and if it is detach it, so can update it.
         Animal local = petShop.Set <Animal>().Local.FirstOrDefault(entry => entry.AnimalId.Equals(animal.AnimalId));
         if (local != null)
         {
             // detach
             petShop.Entry(local).State = EntityState.Detached;
         }
         // set Modified flag in your entry
         petShop.Entry(animal).State = EntityState.Modified;
     }
     else
     {
         await petShop.Animals.AddAsync(animal);;
     }
     await petShop.SaveChangesAsync();
 }
コード例 #8
0
        public async Task <IActionResult> Post([FromBody] CarShopping value)
        {
            using (var _db = new PetShopContext())
            {
                _db.Add(new CarShopping
                {
                    IdUser    = value.IdUser,
                    IdProduct = value.IdProduct
                });
                await _db.SaveChangesAsync();

                return(Ok(value));
            }
        }
コード例 #9
0
        public async Task <Guid> Handle(CreateProductOrderCommand request, CancellationToken cancellationToken)
        {
            var customer = await _context.Customers.FindAsync(request.CustomerId);

            var product = await _context.Products.FindAsync(request.ProductId);

            if (customer is null || product is null)
            {
                return(await Task.FromResult(Guid.Empty));
            }

            var order = new Order
            {
                CustomerId  = customer.Id,
                OrderPlaced = DateTime.UtcNow,
            };

            await _context.Orders.AddAsync(order, cancellationToken);

            var orderValidator        = new OrderValidator();
            var orderValidationResult = await orderValidator.ValidateAsync(order, cancellationToken);

            if (!orderValidationResult.IsValid)
            {
                return(await Task.FromResult(Guid.Empty));
            }

            var productOrder = new ProductOrder
            {
                Quantity  = request.Quantity,
                OrderId   = order.Id,
                ProductId = product.Id,
            };

            await _context.ProductOrders.AddAsync(productOrder, cancellationToken);

            var productOrderValidator        = new ProductOrderValidator();
            var productOrderValidationResult =
                await productOrderValidator.ValidateAsync(productOrder, cancellationToken);

            if (!productOrderValidationResult.IsValid)
            {
                return(await Task.FromResult(Guid.Empty));
            }

            await _context.SaveChangesAsync(cancellationToken);

            return(await Task.FromResult(productOrder.Id));
        }
コード例 #10
0
        public async Task <IActionResult> Post(Purchale value)
        {
            using (var _db = new PetShopContext())
            {
                var date = DateTime.Now;
                _db.Add(new Purchale
                {
                    Date      = date,
                    IdUser    = value.IdUser,
                    IdProduct = value.IdProduct
                });
                await _db.SaveChangesAsync();

                return(Ok(value));
            }
        }
コード例 #11
0
        public async Task <bool> Handle(DeleteProductCommand request, CancellationToken cancellationToken)
        {
            var product = await _context.Products.FirstOrDefaultAsync(
                r => request.Id.Equals(r.Id) && r.DeletedDateUtc == null, cancellationToken);

            if (product is null)
            {
                return(await Task.FromResult(false));
            }

            product.DeletedDateUtc = DateTime.UtcNow;

            await _context.SaveChangesAsync(CancellationToken.None);

            return(await Task.FromResult(true));
        }
コード例 #12
0
        public async Task <bool> Handle(DeleteCustomerCommand request, CancellationToken cancellationToken)
        {
            var customer =
                await _context.Customers.FirstOrDefaultAsync(
                    x => request.Id.Equals(x.Id) && x.DeletedDateUtc == null, cancellationToken);

            if (customer is null)
            {
                return(await Task.FromResult(false));
            }

            customer.DeletedDateUtc = DateTime.UtcNow;

            await _context.SaveChangesAsync(cancellationToken);

            return(await Task.FromResult(true));
        }
コード例 #13
0
        public async Task <IActionResult> Post([FromBody] Product value)
        {
            using (var _db = new PetShopContext())
            {
                _db.Add(new Product
                {
                    Name        = value.Name,
                    Stock       = value.Stock,
                    Description = value.Description,
                    Photo       = value.Photo,
                    Price       = value.Price
                });
                await _db.SaveChangesAsync();

                return(Ok(value));
            }
        }
コード例 #14
0
        public async Task <IActionResult> Delete(int id)
        {
            using (var _db = new PetShopContext())
            {
                try
                {
                    var purchale = await _db.Purchale.FindAsync(id);

                    _db.Remove(purchale);
                    await _db.SaveChangesAsync();

                    return(Ok());
                }
                catch (DbUpdateConcurrencyException)
                {
                    return(BadRequest());
                }
            }
        }
コード例 #15
0
        public async Task <bool> Handle(DeleteProductOrderCommand request, CancellationToken cancellationToken)
        {
            var productOrder = await _context.ProductOrders
                               .Include(p => p.Order.Customer)
                               .Include(p => p.Product)
                               .FirstOrDefaultAsync(p => request.Id.Equals(p.Id) && p.DeletedDateUtc == null, cancellationToken);

            if (productOrder is null)
            {
                return(await Task.FromResult(false));
            }

            var utcNow = DateTime.UtcNow;

            productOrder.DeletedDateUtc       = utcNow;
            productOrder.Order.DeletedDateUtc = utcNow;

            await _context.SaveChangesAsync(cancellationToken);

            return(await Task.FromResult(true));
        }
コード例 #16
0
        public async Task <IActionResult> Put(int id, [FromBody] Product value)
        {
            using (var _db = new PetShopContext())
            {
                try
                {
                    var Product = await _db.Product.FindAsync(id);

                    Product.Name        = value.Name;
                    Product.Stock       = value.Stock;
                    Product.Description = value.Description;
                    Product.Photo       = value.Photo;
                    Product.Price       = value.Price;
                    await _db.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    return(BadRequest());
                }

                return(Ok());
            }
        }
コード例 #17
0
 public async Task <bool> SaveChangeAsync()
 {
     return((await _context.SaveChangesAsync()) > 0);
 }
コード例 #18
0
 public async Task <bool> Register(DTO.Principal p)
 {
     _context.Principal.Add(p);
     return(await _context.SaveChangesAsync() != 0);
 }