public async Task CreateCustomerAsync(CustomerIM item) { var customer = _mapper.Map <Customer>(item); customer.CreatedDate = DateTime.Now; customer.CreatedBy = await _sessionStorageService.GetItemAsync <string>("username"); await _db.AddAsync(customer); await _db.SaveChangesAsync(); }
public async Task UpdateCustomerAsync(int id, CustomerIM item) { var origin = await _db.Customers.FindAsync(id); var customer = _mapper.Map(item, origin); //customer.CreatedDate = DateTime.Now; //customer.CreatedBy = await _sessionStorageService.GetItemAsync<string>("username"); _db.Update(customer); await _db.SaveChangesAsync(); }
public async Task <IActionResult> PostCustomer([FromBody] CustomerIM customer) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var vm = _mapper.Map <Customer>(customer); _context.Customers.Add(vm); await _context.SaveChangesAsync(); return(CreatedAtAction("GetCustomer", new { id = customer.Id }, customer)); }
public async Task <IActionResult> PutCustomer([FromRoute] int id, [FromBody] CustomerIM customer) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != customer.Id) { return(BadRequest("不是有效的Id")); } var model = await _context.Customers.FirstOrDefaultAsync(d => d.Id == id); model = _mapper.Map(customer, model); _context.Entry(model).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CustomerExists(id)) { return(NotFound()); } else { throw; } } return(Ok()); }