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> GetCustomerShoppingArea([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); var customerShoppingArea = await _context.CustomerShoppingArea.SingleOrDefaultAsync(c => c.CustomerId == customer.CustomerId& c.ShoppingAreaId == shoppingArea.ShoppingAreaId); if (customerShoppingArea == null) { return(NotFound()); } return(Ok(customerShoppingArea)); }
public async Task <IActionResult> GetCustomerShoppingArea([FromRoute] int updateStatus) { var qry = _context.CustomerShoppingArea.AsNoTracking().Where(c => c.UpdateStatus == updateStatus) .Include(c => c.Customer) .Include(c => c.ShoppingArea); var items = await qry.ToListAsync(); List <CustomerShoppingAreaCode> customerShoppingAreaCodes = new List <CustomerShoppingAreaCode>(); foreach (var item in items) { CustomerShoppingAreaCode customerShoppingAreaCode = new CustomerShoppingAreaCode { ShoppingAreaCode = item.ShoppingArea.Code, CustomerCode = item.Customer.Code }; customerShoppingAreaCodes.Add(customerShoppingAreaCode); } return(Ok(customerShoppingAreaCodes)); }