public async Task <IActionResult> Delete([FromRoute, Required] string id) { try { if (string.IsNullOrWhiteSpace(id)) { return(BadRequest("Store ID is required!")); } var getRes = Uow.StoreRepository.Get(id); if (getRes != null) { Uow.StoreRepository.Remove(getRes); await Uow.CompleteAsync(); return(NoContent()); } return(NotFound()); } catch (Exception e) { Console.WriteLine(e); return(InternalServerError()); } }
public async Task <IActionResult> Delete([FromRoute, Required] string id) { try { if (String.IsNullOrWhiteSpace(id)) { return(BadRequest("Id must be specified")); } var order = Uow.OrderRepository.Get(id); if (order == null) { return(NotFound("Product with given Id was not found!")); } Uow.OrderRepository.Remove(order); await Uow.CompleteAsync(); return(NoContent()); } catch (Exception e) { Console.WriteLine(e); return(InternalServerError()); } }
public async Task <IActionResult> Update([FromBody] UpdateOrderModel updateOrderModel) { try { if (string.IsNullOrWhiteSpace(updateOrderModel.State)) { return(BadRequest("State cannot be null!")); } var order = Uow.OrderRepository.Get(updateOrderModel.Id); if (order == null) { return(NotFound("Order with given Id was not found!")); } var entity = Mapper.Map(updateOrderModel, order); var updatedOrder = Uow.OrderRepository.Update(entity); await Uow.CompleteAsync(); return(Ok(Mapper.Map <OrderModel>(Mapper.Map <OrderModel>(updatedOrder)))); } catch (Exception e) { Console.WriteLine(e); return(InternalServerError()); } }
public async Task Add(Product product) { var validReturn = await _validator.ValidateAsync(product); if (!validReturn.IsValid) { throw new Domain.Exceptions.ValidationException(validReturn.Errors.ToList()); } Uow.Products.Create(product); await Uow.CompleteAsync(); }
public async Task <IActionResult> Update([FromBody] UpdateProductModel updateProductModel) { try { if (String.IsNullOrWhiteSpace(updateProductModel.Name) || String.IsNullOrWhiteSpace(updateProductModel.Description) || String.IsNullOrWhiteSpace(updateProductModel.StoreId) || String.IsNullOrWhiteSpace(updateProductModel.QuantityUnit)) { return(BadRequest("At least one of required fields are empty!")); } if (updateProductModel.Price <= 0) { return(BadRequest("Price must be greater than 0!")); } if (updateProductModel.Quantity < 0) { return(BadRequest("Quantity cannot be less than 0!")); } var product = Uow.ProductRepository.Get(updateProductModel.Id); if (product == null) { return(NotFound("Product with given Id was not found!")); } var store = Uow.StoreRepository.Get(updateProductModel.StoreId); if (store == null) { return(NotFound("Store with given Id was not found!")); } var entity = Mapper.Map(updateProductModel, product); var updatedProduct = Uow.ProductRepository.Update(entity); await Uow.CompleteAsync(); return(Ok(Mapper.Map <ProductModel>(Mapper.Map <ProductModel>(updatedProduct)))); } catch (Exception e) { Console.WriteLine(e); return(InternalServerError()); } }
public async Task <IActionResult> Create([FromBody] CreateProductModel createProductModel) { try { var storeId = User.Claims.FirstOrDefault(x => x.Type == "StoreId")?.Value; if (String.IsNullOrWhiteSpace(storeId)) { return(BadRequest("No such store id!")); } if (String.IsNullOrWhiteSpace(createProductModel.Name) || String.IsNullOrWhiteSpace(createProductModel.Description) || String.IsNullOrWhiteSpace(createProductModel.QuantityUnit)) { return(BadRequest("At least one of required fields are empty!")); } if (createProductModel.Price <= 0) { return(BadRequest("Price must be greater than 0")); } var store = Uow.StoreRepository.Get(storeId); if (store == null) { return(NotFound("Store with given Id was not found!")); } var product = Mapper.Map <Product>(createProductModel); product.Quantity = 0; product.StoreId = storeId; var entity = Uow.ProductRepository.Add(product); await Uow.CompleteAsync(); return(Created(Mapper.Map <ProductModel>(entity))); } catch (Exception e) { Console.WriteLine(e); return(InternalServerError(e.ToString())); } }
public async Task <IActionResult> Update([FromBody] UpdateStoreModel updateStoreModel) { try { var currentUserId = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value; var currentUserRole = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role)?.Value; var currentUserStoreId = User.Claims.FirstOrDefault(x => x.Type == "StoreId")?.Value; if (string.IsNullOrWhiteSpace(updateStoreModel.Id)) { return(BadRequest("Store ID is required!")); } if (currentUserRole == Roles.Owner && currentUserStoreId != updateStoreModel.Id) { return(Forbidden()); } if (string.IsNullOrWhiteSpace(updateStoreModel.Name) || string.IsNullOrWhiteSpace(updateStoreModel.Address)) { return(BadRequest("Store Name and Address is required!")); } var getResponse = Uow.StoreRepository.Get(updateStoreModel.Id); if (getResponse == null) { return(NotFound("Store with given ID does not exist!")); } var entity = Mapper.Map(updateStoreModel, getResponse); var updateResponse = Uow.StoreRepository.Update(entity); await Uow.CompleteAsync(); return(Ok(Mapper.Map <StoreModel>(updateResponse))); } catch (Exception e) { Console.WriteLine(e); return(InternalServerError()); } }
public async Task <IActionResult> Add([FromBody] CreateStoreModel createStoreModel) { try { if (string.IsNullOrWhiteSpace(createStoreModel.Name) || string.IsNullOrWhiteSpace(createStoreModel.Address)) { return(BadRequest("Store Name and Address is required!")); } var getRes = Uow.StoreRepository.Find(x => x.Name == createStoreModel.Name && x.Address == createStoreModel.Address); if (getRes != null) { return(Conflict("Store with given name nad address already exists!")); } var addResponse = Uow.StoreRepository.Add(Mapper.Map <Store>(createStoreModel)); await Uow.CompleteAsync(); if (addResponse != null) { return(Created(Mapper.Map <StoreModel>(addResponse))); } return(InternalServerError()); } catch (Exception e) { Console.WriteLine(e); return(InternalServerError()); } }
public async Task <IActionResult> Create([FromBody] CreateOrderModel createOrderModel) { try { var currentUserId = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value; if (String.IsNullOrWhiteSpace(createOrderModel.StoreId)) { return(BadRequest("Store ID is required!")); } if (createOrderModel.Products == null || !createOrderModel.Products.Any()) { return(BadRequest("Product list cannot be null!")); } var store = Uow.StoreRepository.Query(x => x.Id == createOrderModel.StoreId, null, i => i.Products) .FirstOrDefault(); var user = Uow.UserRepository.Get(currentUserId); if (user == null || store == null) { return(NotFound("Such user or store doesn't exist!")); } var order = Mapper.Map <Order>(createOrderModel); order.State = "Zamowienie przyjete!"; order.TimeCreated = DateTime.UtcNow; order.UserId = currentUserId; double totalOrderValue = 0d; var storeProducts = new List <Product>(); var orderProducts = new List <OrderProduct>(); foreach (var product in createOrderModel.Products) { var storeProduct = store.Products.FirstOrDefault(x => x.Id == product.Id); if (storeProduct == null) { return(BadRequest($"Product with ID: {product.Id} does not exist!")); } if (storeProduct.Quantity < product.Quantity) { return(BadRequest($"You can't buy more {storeProduct.Name} than we have!")); } storeProducts.Add(storeProduct); storeProduct.Quantity = storeProduct.Quantity - product.Quantity; Uow.ProductRepository.Update(storeProduct); orderProducts.Add(new OrderProduct() { ProductId = product.Id, Quantity = product.Quantity, PriceEach = storeProduct.Price, QuantityUnit = storeProduct.QuantityUnit }); totalOrderValue += product.Quantity * storeProduct.Price; } var orderEntity = Mapper.Map <Order>(createOrderModel); orderEntity.OrderProducts = orderProducts; Uow.OrderRepository.Add(orderEntity); var entity = Uow.OrderRepository.Add(order); await Uow.CompleteAsync(); var res = Mapper.Map <OrderModel>(entity); res.Products = storeProducts.Select(Mapper.Map <ProductModel>); res.TotalOrderValue = totalOrderValue; return(Created(res)); } catch (Exception e) { Console.WriteLine(e); return(InternalServerError()); } }
public async Task <IActionResult> Create([FromBody] CreateUserModel createUserModel) { try { if (string.IsNullOrWhiteSpace(createUserModel.Email) || string.IsNullOrWhiteSpace(createUserModel.Password)) { return(BadRequest()); } var currentUserId = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value; var currentUserRole = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Role)?.Value; var getUserRes = Uow.UserRepository.Find(x => x.Email == createUserModel.Email); if (getUserRes != null) { return(Conflict("User with given email is already registered!")); } if (string.IsNullOrWhiteSpace(currentUserId)) { var userEntity = new User { Email = createUserModel.Email, Password = _passwordHasher.Hash(createUserModel.Password), Role = Roles.User }; var res = Uow.UserRepository.Add(userEntity); await Uow.CompleteAsync(); return(Created(Mapper.Map <UserModel>(res))); } if (currentUserRole == Roles.User) { return(BadRequest("You are already registered!")); } if (currentUserRole == Roles.Admin) { if (string.IsNullOrWhiteSpace(createUserModel.Role)) { return(BadRequest("Role is required!")); } var getStoreRes = Uow.StoreRepository.Get(createUserModel.StoreId); if (getStoreRes == null) { return(BadRequest("Store with given id does not exist.")); } var entity = new User { Email = createUserModel.Email, Password = _passwordHasher.Hash(createUserModel.Password), StoreId = createUserModel.StoreId, Role = createUserModel.Role, }; var res = Uow.UserRepository.Add(entity); await Uow.CompleteAsync(); return(Created(Mapper.Map <UserModel>(res))); } if (currentUserRole == Roles.Owner) { var currentUser = Uow.UserRepository.Get(currentUserId); if (currentUser == null) { return(InternalServerError()); } var entity = new User { StoreId = currentUser.StoreId, Email = createUserModel.Email, Password = _passwordHasher.Hash(createUserModel.Password), Role = Roles.Employee }; var res = Uow.UserRepository.Add(entity); await Uow.CompleteAsync(); return(Created(Mapper.Map <UserModel>(res))); } return(InternalServerError()); } catch (Exception e) { Console.WriteLine(e); return(InternalServerError()); } }
public async Task Update(Product product) { Uow.Products.Update(product); await Uow.CompleteAsync(); }