public async Task<HttpResponseMessage> NewStaffAssignment(AccountRoleRequest accountRoleRequest) { Services.Log.Info("New Staff Assignment Request [API]"); string responseText; stranddContext context = new stranddContext(); //Checks for existing Link References Account lookupAccount = await context.Accounts.Where(a => a.ProviderUserID == accountRoleRequest.UserProviderID).SingleOrDefaultAsync(); Company lookupCompany = await context.Companies.Where(a => a.Id == accountRoleRequest.CompanyGUID).SingleOrDefaultAsync(); if (lookupAccount == null) { responseText = "Account not found [" + accountRoleRequest.UserProviderID + "]"; Services.Log.Warn(responseText); return this.Request.CreateResponse(HttpStatusCode.BadRequest, responseText); } if (lookupCompany == null) { responseText = "Company not found [" + accountRoleRequest.CompanyGUID + "]"; Services.Log.Warn(responseText); return this.Request.CreateResponse(HttpStatusCode.BadRequest, responseText); } if (accountRoleRequest.RoleAssignment == null) { responseText = "No Role Assignment Defined"; Services.Log.Warn(responseText); return this.Request.CreateResponse(HttpStatusCode.BadRequest, responseText); } AccountRole lookupAccountRole = await context.AccountRoles.Where(a => a.UserProviderID == accountRoleRequest.UserProviderID) .Where(b => b.CompanyGUID == accountRoleRequest.CompanyGUID) .Where(c => c.RoleAssignment == accountRoleRequest.RoleAssignment).SingleOrDefaultAsync(); if (lookupAccountRole == null) { //Staff Assignment Creation AccountRole newAccountRole = new AccountRole { Id = Guid.NewGuid().ToString(), RoleAssignment = accountRoleRequest.RoleAssignment, CompanyGUID = accountRoleRequest.CompanyGUID, UserProviderID = accountRoleRequest.UserProviderID }; context.AccountRoles.Add(newAccountRole); await context.SaveChangesAsync(); responseText = "Staff Assignment Successfully Generated"; Services.Log.Info(responseText); return this.Request.CreateResponse(HttpStatusCode.Created, responseText); } else { responseText = "Staff Assignment Already Exists"; Services.Log.Info(responseText); return this.Request.CreateResponse(HttpStatusCode.OK, responseText); } }
public async Task<HttpResponseMessage> RemoveStaffAssignment(AccountRoleRequest accountRoleRequest) { Services.Log.Info("Remove Staff Assignment Request [API]"); string responseText; stranddContext context = new stranddContext(); if (accountRoleRequest.RoleAssignment == null) { responseText = "No Role Assignment Defined"; Services.Log.Warn(responseText); return this.Request.CreateResponse(HttpStatusCode.BadRequest, responseText); } AccountRole lookupAccountRole = await context.AccountRoles.Where(a => a.UserProviderID == accountRoleRequest.UserProviderID) .Where(b => b.CompanyGUID == accountRoleRequest.CompanyGUID) .Where(c => c.RoleAssignment == accountRoleRequest.RoleAssignment).SingleOrDefaultAsync(); if (lookupAccountRole == null) { responseText = "Staff Assignment Not Found"; Services.Log.Info(responseText); return this.Request.CreateResponse(HttpStatusCode.NotFound, responseText); } else { //Staff Assignment Removal context.AccountRoles.Remove(lookupAccountRole); await context.SaveChangesAsync(); responseText = "Staff Assignment Successfully Removed"; Services.Log.Info(responseText); return this.Request.CreateResponse(HttpStatusCode.Created, responseText); } }