public IActionResult Update(int id, [FromBody] ProductServiceDto productService, [FromHeader] int UserID, [FromHeader] string UserRole) { var currentProductService = _context.ProductServices.Find(id); if (currentProductService == null) { return(StatusCode(StatusCodes.Status404NotFound)); } if (currentProductService.CreatedByUserID != UserID && UserRole != "Admin") { return(StatusCode(StatusCodes.Status403Forbidden)); } currentProductService.Title = productService.Title; currentProductService.Text = productService.Text; currentProductService.PriceAgreement = productService.PriceAgreement; currentProductService.IsPriceChangeable = productService.IsPriceChangeable; currentProductService.Exchangement = productService.Exchangement; currentProductService.ExchangementCondition = productService.ExchangementCondition; _context.ProductServices.Update(currentProductService); var success = _context.SaveChanges(); if (success < 1) { return(StatusCode(StatusCodes.Status400BadRequest)); } this._loggerCommunication.logAction("Updated a product/service with id:" + id); return(StatusCode(StatusCodes.Status202Accepted, new JsonResult(currentProductService))); }
public IActionResult Create([FromBody] ProductServiceDto productService, [FromHeader] int UserID) { var newProductService = new ProductService() { CreatedByUserID = UserID, Title = productService.Title, Text = productService.Text, PriceAgreement = productService.PriceAgreement, IsPriceChangeable = productService.IsPriceChangeable, Exchangement = productService.Exchangement, ExchangementCondition = productService.ExchangementCondition }; _context.ProductServices.Add(newProductService); var success = _context.SaveChanges(); if (success < 1) { return(StatusCode(StatusCodes.Status400BadRequest)); } this._loggerCommunication.logAction("Created a new product/service"); return(StatusCode(StatusCodes.Status201Created, new JsonResult(newProductService))); }