// DELETE id /// <summary> /// Removing a product /// </summary> /// <param name="id">Id of the product</param> public IHttpActionResult Delete(int id) { System.Web.Http.Results.StatusCodeResult status; using (IUMdbEntities entities = new IUMdbEntities()) { Products deletedProduct = entities.Products.Find(id); if (deletedProduct == null) { status = new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.NotFound, this); return(status); } List <Prices> pricesOfDeletedProduct = entities.Prices.Where(price => price.ProductId == deletedProduct.Id).ToList(); foreach (Prices price in pricesOfDeletedProduct) { entities.Prices.Remove(price); } entities.SaveChanges(); entities.Products.Remove(deletedProduct); entities.SaveChanges(); status = new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.OK, this); return(status); } }
// PUT id?quantityChange /// <summary> /// Change quantity of given product /// </summary> /// <param name="id">Id of the product</param> /// <param name="quantityChange">Change in quantity</param> public IHttpActionResult Put(int id, int quantityChange) { System.Web.Http.Results.StatusCodeResult status; using (IUMdbEntities entities = new IUMdbEntities()) { Products changedProduct = entities.Products.Find(id); if (changedProduct == null) { status = new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.NotFound, this); return(status); } if ((changedProduct.Quantity + quantityChange) < 0) { status = new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.Conflict, this); return(status); } if (changedProduct != null) { changedProduct.Quantity += quantityChange; entities.Products.AddOrUpdate(changedProduct); entities.SaveChanges(); status = new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.OK, this); return(status); } else { status = new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.NotFound, this); return(status); } } }
public IHttpActionResult Register([FromBody] Users user) { System.Web.Http.Results.StatusCodeResult status; using (IUMdbEntities entities = new IUMdbEntities()) { #region Validation #region checkIfUserIsNull if (user == null) { status = new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.Conflict, this); return(status); } #endregion #region checkIfUsernameIsTaken bool checkIfUsernameIsTaken = entities.Users .Any(e => e.Username == user.Username); if (checkIfUsernameIsTaken) { status = new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.Conflict, this); return(status); } #endregion #endregion CryptoService cryptoService = new CryptoService(); string hashedPassword = cryptoService.GetHashedString(user.Password); Users newUser = new Users() { Username = user.Username, Password = hashedPassword, GoogleId = user.GoogleId }; entities.Users.Add(newUser); entities.SaveChanges(); Users dbUser = entities.Users .Where(e => e.Username == user.Username) .First(); UserRoles employeeRole = entities.UserRoles .Where(e => e.RoleName == "Employee") .First(); Workplaces newWorkplace = new Workplaces() { UserId = dbUser.Id, UserRoleId = employeeRole.Id }; entities.Workplaces.Add(newWorkplace); entities.SaveChanges(); status = new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.Created, this); return(status); } }
// PUT /// <summary> /// Edit all availble product fields (exluding Quantiy) /// </summary> /// <param name="product">Updated product</param> public IHttpActionResult Put([FromBody] Products product, string CountryContext = "PLN") { System.Web.Http.Results.StatusCodeResult status; using (IUMdbEntities entities = new IUMdbEntities()) { Products changedProduct = entities.Products.Find(product.Id); if (changedProduct == null) { status = new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.NotFound, this); return(status); } Countries requestedCountry = entities.Countries.FirstOrDefault(country => country.CountryTag == CountryContext); Prices changedPrice = entities.Prices.FirstOrDefault(price => price.ProductId == changedProduct.Id && price.CountryId == requestedCountry.Id); if (changedPrice == null) { changedPrice = new Prices() { ProductId = changedProduct.Id, CountryId = requestedCountry.Id }; } changedPrice.Price = product.Price.Value; entities.Prices.AddOrUpdate(changedPrice); entities.SaveChanges(); product.Price = null; product.Quantity = changedProduct.Quantity; if (product.OriginCountry == null) { product.OriginCountry = changedProduct.OriginCountry; } entities.Products.AddOrUpdate(product); entities.SaveChanges(); status = new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.OK, this); return(status); } }
// POST /// <summary> /// Add new product. New products should be added with Quantity=0 /// </summary> /// <param name="product">Product to add to database</param> public IHttpActionResult Post([FromBody] Products product, string CountryContext = "PLN") { System.Web.Http.Results.StatusCodeResult status; product.Quantity = 0; using (IUMdbEntities entities = new IUMdbEntities()) { bool checkIfProductExist = entities.Products.Any(e => e.ManufacturerName == product.ManufacturerName && e.ModelName == product.ModelName); /* * bool checkIfCountryContextIsSupported = entities.Countries.Any(e => * e.CountryTag == CountryContext); */ if (checkIfProductExist) { status = new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.Conflict, this); return(status); } entities.Products.Add(product); entities.SaveChanges(); Countries requestedCountry = entities.Countries.FirstOrDefault(country => country.CountryTag == CountryContext); Prices price = new Prices() { Price = product.Price.Value, CountryId = requestedCountry.Id, ProductId = product.Id }; PricesController pc = new PricesController(); pc.Post(price); status = new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.Created, this); return(status); } }
// POST /// <summary> /// Add new price for a product. /// </summary> /// <param name="product">Price to add to database</param> public IHttpActionResult Post([FromBody] Prices price) { System.Web.Http.Results.StatusCodeResult status; using (IUMdbEntities entities = new IUMdbEntities()) { bool checkIfPriceExist = entities.Prices.Any(e => e.ProductId == price.ProductId && e.CountryId == price.Id); if (checkIfPriceExist) { status = new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.Conflict, this); return(status); } entities.Prices.Add(price); entities.SaveChanges(); status = new System.Web.Http.Results.StatusCodeResult(HttpStatusCode.Created, this); return(status); } }