public async Task <IActionResult> AddCustomerShoppingArea([FromBody] Customer customer) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await _context.Users.SingleAsync(x => x.UserName == HttpContext.User.Identity.Name); if (user == null) { ModelState.AddModelError("", "Sory, your user is empty !"); return(BadRequest(ModelState)); } UserMeta userMeta = _context.UserMeta.FirstOrDefault(u => u.Id == user.UserMetaId); ShoppingArea shoppingArea = await _context.ShoppingArea.FirstOrDefaultAsync(x => x.ShoppingAreaId == userMeta.ShoppingAreaId); if (shoppingArea == null) { ModelState.AddModelError("", "Sory, your Shoping Area is empty !"); return(BadRequest(ModelState)); } _context.AttachRange(customer); CustomerShoppingArea customerShoppingArea = _context.CustomerShoppingArea.FirstOrDefault(c => c.CustomerId == customer.CustomerId & c.ShoppingAreaId == shoppingArea.ShoppingAreaId); if (customerShoppingArea != null) { customerShoppingArea.ShoppingArea = shoppingArea; customerShoppingArea.Customer = customer; customerShoppingArea.UpdateStatus = 1; await _context.SaveChangesAsync(); return(CreatedAtAction("GetCustomerShoppingArea", new CustomerShoppingAreaCode { ShoppingAreaCode = shoppingArea.Code, CustomerCode = customer.Code })); } else { customerShoppingArea = new CustomerShoppingArea(); customerShoppingArea.ShoppingArea = shoppingArea; customerShoppingArea.Customer = customer; customerShoppingArea.UpdateStatus = 1; _context.CustomerShoppingArea.Add(customerShoppingArea); await _context.SaveChangesAsync(); return(CreatedAtAction("GetCustomerShoppingArea", new CustomerShoppingAreaCode { ShoppingAreaCode = shoppingArea.Code, CustomerCode = customer.Code })); } }
public async Task <IActionResult> PostCustomerShoppingArea([FromBody] CustomerShoppingAreaCode customerShoppingAreaCode) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Customer customer = _context.Customers.FirstOrDefault(p => p.Code == customerShoppingAreaCode.CustomerCode); ShoppingArea shoppingArea = _context.ShoppingArea.FirstOrDefault(p => p.Code == customerShoppingAreaCode.ShoppingAreaCode); if (customer == null) { return(BadRequest(ModelState)); } if (shoppingArea == null) { return(BadRequest(ModelState)); } CustomerShoppingArea customerShoppingArea = _context.CustomerShoppingArea.FirstOrDefault(c => c.CustomerId == customer.CustomerId & c.ShoppingAreaId == shoppingArea.ShoppingAreaId); if (customerShoppingArea != null) { customerShoppingArea.ShoppingArea = shoppingArea; customerShoppingArea.Customer = customer; customerShoppingArea.UpdateStatus = 0; await _context.SaveChangesAsync(); return(CreatedAtAction("GetCustomerShoppingArea", new CustomerShoppingAreaCode { ShoppingAreaCode = shoppingArea.Code, CustomerCode = customer.Code })); } else { customerShoppingArea = new CustomerShoppingArea(); customerShoppingArea.ShoppingArea = shoppingArea; customerShoppingArea.Customer = customer; customerShoppingArea.UpdateStatus = 0; _context.CustomerShoppingArea.Add(customerShoppingArea); await _context.SaveChangesAsync(); return(CreatedAtAction("GetCustomerShoppingArea", new CustomerShoppingAreaCode { ShoppingAreaCode = shoppingArea.Code, CustomerCode = customer.Code })); } }
public async Task <IActionResult> DeleteCustomerShoppingArea([FromBody] Customer customer) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = await _context.Users.SingleAsync(x => x.UserName == HttpContext.User.Identity.Name); if (user == null) { ModelState.AddModelError("", "Sory, your user is empty !"); return(BadRequest(ModelState)); } UserMeta userMeta = _context.UserMeta.FirstOrDefault(u => u.Id == user.UserMetaId); ShoppingArea shoppingArea = await _context.ShoppingArea.FirstOrDefaultAsync(x => x.ShoppingAreaId == userMeta.ShoppingAreaId); if (shoppingArea == null) { ModelState.AddModelError("", "Sory, your Shoping Area is empty !"); return(BadRequest(ModelState)); } CustomerShoppingArea customerShoppingArea = _context.CustomerShoppingArea.FirstOrDefault(c => c.ShoppingAreaId == userMeta.ShoppingAreaId && c.CustomerId == customer.CustomerId); if (customerShoppingArea != null) { customerShoppingArea.UpdateStatus = 2; await _context.SaveChangesAsync(); } return(Ok()); }