public async Task AddUserToPractice(string id, int practiceId) { var subPrac = new SubscriberPractice() { PracticeId = practiceId, SubscriberUserId = id }; _context.SubscriberPractices.Add(subPrac); await _context.SaveChangesAsync(); }
public async Task <IActionResult> RemoveUserFromPractice([FromBody] SubscriberPractice subscriberPractice) { if (subscriberPractice == null || string.IsNullOrEmpty(subscriberPractice.SubscriberUserId) || subscriberPractice.PracticeId == 0) { return(Json(new JsonResponse(false, "Server did not receive data."))); } try { return(Json(await _practicesService.RemoveSubscriberFromPractice(subscriberPractice))); } catch { return(Json(new JsonResponse(false, "Concurrency error."))); } }
public async Task <IActionResult> MakeUserManager([FromBody] SubscriberPractice subscriberPractice) { using (var transact = await _context.Database.BeginTransactionAsync()) { try { if (subscriberPractice == null || string.IsNullOrEmpty(subscriberPractice.SubscriberUserId) || subscriberPractice.PracticeId == 0) { return(Json(new JsonResponse(false, "Server did not receive data."))); } var response = await _practicesService.UpdateUserManagerStatus(subscriberPractice); if (response.Success) { IdentityResult roleChange; if (subscriberPractice.IsManager) { roleChange = await _userManager.AddToRoleAsync(await _adminService.GetUserById(subscriberPractice.SubscriberUserId), "PracticeManager"); } else { roleChange = await _userManager.RemoveFromRoleAsync(await _adminService.GetUserById(subscriberPractice.SubscriberUserId), "PracticeManager"); } if (roleChange.Succeeded) { transact.Commit(); return(Json(new JsonResponse())); } transact.Rollback(); return(Json(new JsonResponse(false, "Could not add user role."))); } transact.Rollback(); return(Json(new JsonResponse(false, "COuld not update subscriber practice."))); } catch { transact.Rollback(); return(Json(new JsonResponse(false, "Concurrency error."))); } } }
public async Task <JsonResponse> UpdateUserManagerStatus(SubscriberPractice subscriberPractice) { try { var subPrac = await _context .SubscriberPractices .Where( sp => sp.PracticeId == subscriberPractice.PracticeId && sp.SubscriberUserId == subscriberPractice.SubscriberUserId) .SingleAsync(); subPrac.IsManager = subscriberPractice.IsManager; await _context.SaveChangesAsync(); return(new JsonResponse()); } catch { return(new JsonResponse(false, "An error occurred removing the entity.")); } }
public async Task <JsonResponse> RemoveSubscriberFromPractice(SubscriberPractice subscriberPractice) { try { _context.SubscriberPractices.Remove( await _context .SubscriberPractices .Where( sp => sp.PracticeId == subscriberPractice.PracticeId && sp.SubscriberUserId == subscriberPractice.SubscriberUserId) .SingleAsync() ); await _context.SaveChangesAsync(); return(new JsonResponse()); } catch { return(new JsonResponse(false, "An error occurred removing the entity.")); } }