public async Task<IHttpActionResult> PostUser(CustomerModel postCust) { if (!ModelState.IsValid) { /*{"UserName":"******", "FullName":"cust.FullName", "Phone":"cust.Phone", "CompanyName":"cust.CompanyName", "Email":"cust.Email", "Hd_Id":? }*/ return BadRequest(ModelState); } try { postCust = await _repository.Add(postCust); } catch (DbUpdateException msgDbUpdateException) { return InternalServerError(msgDbUpdateException); } return CreatedAtRoute("DefaultApi", new { id = postCust.Id }, postCust); }
public IEnumerable<CustomerModel> GetByHd(short hdId) { var list = new List<CustomerModel>(); var custs = _db.Customers.Where(c => c.Hd_Id == hdId); if (!custs.Any()) return null; foreach (var cust in custs) { var userIden = UserManager.FindById(cust.Asp_Id); var custModel = new CustomerModel { Id = cust.Id, UserName = userIden.UserName, FullName = cust.FullName, Phone = cust.Phone, CompanyName = cust.CompanyName, Email = cust.Email, Hd_Id = cust.Hd_Id }; list.Add(custModel); } return list; }
public async Task<CustomerModel> Add(CustomerModel item) { //add to asp.net Identity var usrIden = new IdentityUser(item.UserName); var usrResult = await UserManager.CreateAsync(usrIden, item.UserName);//pass is same username **by default** if (!usrResult.Succeeded) { throw new DbUpdateException(usrResult.Errors.First()); } var roleResult = await UserManager.AddToRoleAsync(usrIden.Id, "customer"); if (!roleResult.Succeeded) { throw new DbUpdateException(roleResult.Errors.First()); } var newCust = new Customer { Hd_Id = item.Hd_Id, Asp_Id = usrIden.Id, CompanyName=item.CompanyName, Email = item.Email, FullName=item.FullName, Phone = item.Phone }; _db.Customers.Add(newCust); try { await _db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException ex) { //remove asp.net identity user var usr = AspContext.Users.First(u => u.Id == usrIden.Id); //AspContext.Users.Remove(usr); //AspContext.SaveChanges(); if (usr == null) throw new DbUpdateConcurrencyException(ex.Message); AspContext.Users.Remove(usr); try { AspContext.SaveChanges(); } catch (DbUpdateConcurrencyException aspEx) { throw new DbUpdateConcurrencyException(aspEx.Message); } throw new DbUpdateConcurrencyException(ex.Message); } item.Id = newCust.Id; return item; }
public async Task<CustomerModel> Get(int id) { var cust = await _db.Customers.FirstOrDefaultAsync(c => c.Id == id); if (cust == null) throw new KeyNotFoundException("id"); var userIden = await UserManager.FindByIdAsync(cust.Asp_Id); var custModel = new CustomerModel { Id = cust.Id, UserName = userIden.UserName, FullName = cust.FullName, Phone = cust.Phone, CompanyName = cust.CompanyName, Email = cust.Email, Hd_Id = cust.Hd_Id }; return custModel; }
public async Task<IHttpActionResult> PutUser(int id, CustomerModel putCust) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != putCust.Id) { return BadRequest(); } try { await _repository.Update(putCust); } catch (KeyNotFoundException) { return NotFound(); } catch (DbUpdateException mgsDbUpdateException) { return InternalServerError(mgsDbUpdateException); } return StatusCode(HttpStatusCode.NoContent); }
public async Task<bool> Update(CustomerModel item) { var cust = await _db.Customers.FirstOrDefaultAsync(c => c.Id == item.Id); if (cust == null) { throw new KeyNotFoundException("id"); } var header = await _db.Hds.FirstOrDefaultAsync(h => h.Id == item.Hd_Id); if (header == null) { throw new KeyNotFoundException("Hd_Id"); } //edit customer cust.Hd_Id = item.Hd_Id; cust.CompanyName = item.CompanyName; cust.Email = item.Email; cust.FullName = item.FullName; cust.Phone = item.Phone; _db.Entry(cust).State = EntityState.Modified; try { await _db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException ex) { throw new DbUpdateConcurrencyException(ex.Message); } return true; }