public IHttpActionResult AddOrUpdateEmployee(Employee employee, int type) { try { if (ModelState.IsValid) { using (MudarDBContext mdbContext = new MudarDBContext()) { if (employee.EmployeeId == Guid.Empty) { employee.EmployeeId = Guid.NewGuid(); employee.CreatedBy = "superadmin"; employee.CreatedDate = DateTime.Now; var userlogin = new UserLogin() { UserId = employee.EmployeeId, UserLoginId = MudarCommon.GenerateULogin(employee.EmployeeFirstName), UserPassword = MudarCommon.GeneratePassword(employee.EmployeeFirstName), CreatedBy = "superadmin", CreatedDate = DateTime.Now }; var usrRole = new UsersInRole() { UserId = employee.EmployeeId, RoleId = mdbContext.Roles.First(r => r.BranchRoleValue == type).RoleId, CreatedBy = "superadmin", CreatedDate = DateTime.Now }; mdbContext.UserLogins.Add(userlogin); mdbContext.Employees.Add(employee); mdbContext.UserRoles.Add(usrRole); } else { mdbContext.Employees.Attach(employee); mdbContext.Entry <Employee>(employee).State = EntityState.Modified; employee.ModifiedBy = "superadmin"; employee.ModifiedDate = DateTime.Now; } mdbContext.SaveChanges(); } return(Ok("success")); } else { return(BadRequest("failed")); } } catch (Exception) { return(BadRequest("failed")); } }
public IHttpActionResult PostBuyerDetail(BuyerDetail buyerDetail) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } using (var transaction = db.Database.BeginTransaction()) { try { buyerDetail.BuyerId = Guid.NewGuid(); var userlogin = new UserLogin() { UserId = buyerDetail.BuyerId, UserLoginId = MudarCommon.GenerateULogin(buyerDetail.BuyerCompanyName), UserPassword = MudarCommon.GeneratePassword(buyerDetail.BuyerCompanyName), CreatedBy = createdBy, CreatedDate = createdDate }; db.UserLogins.Add(userlogin); buyerDetail.CreatedBy = createdBy; buyerDetail.CreatedDate = createdDate; db.Buyers.Add(buyerDetail); db.SaveChanges(); transaction.Commit(); } catch (DbUpdateException) { transaction.Rollback(); if (BuyerDetailExists(buyerDetail.BuyerId)) { return(Conflict()); } else { return(BadRequest()); } } catch (Exception ex) { transaction.Rollback(); return(BadRequest()); } } return(Ok(buyerDetail.BuyerId)); }