/// <summary> /// 修改資料 /// </summary> /// <param name="requestData"></param> /// <returns></returns> public bool UpdateMutiple(LearningRolePostResquest requestData) { if (requestData.Roles.FirstOrDefault() == null) { return(false); } var tokenService = new TokenService(); var tokenInfo = tokenService.GetTokenInfo(requestData.Token).Result; if (tokenInfo == null) { return(false); } var db = _uow.DbContext; foreach (var learningRole in requestData.Roles) { var oldLeanirngRole = db.LearningRole.Find(learningRole.RoleId); //查不到或無法修改就跳過 if (oldLeanirngRole == null || oldLeanirngRole.IsFixed == false) { continue; } oldLeanirngRole.Name = learningRole.Name; oldLeanirngRole.Level = learningRole.Level; oldLeanirngRole.Updated = TimeData.Create(DateTime.UtcNow); oldLeanirngRole.UpdateUser = tokenInfo.MemberId; } db.SaveChanges(); return(true); }
public IHttpActionResult Put(LearningRolePostResquest requestData) { var requestService = new Service.Utility.RequestDataHelper <Infrastructure.ViewModel.Backend.LearningRolePostResquest>(); var checkColumnKey = new string[3] { "token", "circlekey", "roles" }; var checkEmpty = requestService.CheckColumnEmpty(requestData, checkColumnKey); var response = new Infrastructure.ViewModel.Base.BaseResponse <IEnumerable <LearningRoleGetResponse> >(); response.Success = false; response.Data = new List <LearningRoleGetResponse>(); if (checkEmpty == false) { response.Message = "遺漏參數"; response.State = Infrastructure.ViewModel.Base.LogState.RequestDataError; return(Content(HttpStatusCode.BadRequest, response)); } var authService = new AuthService(); var checkManageAuth = authService.CheckCourseManageAuth(requestData.Token, requestData.CircleKey.ToLower()); //除了課程管理者與老師可以修改,其他角色都不得修改 if (checkManageAuth == null || checkManageAuth.CircleRoleSetting.AddCircleRole == false) { response.Message = "無權限修改"; response.State = Infrastructure.ViewModel.Base.LogState.Error; return(Ok(response)); } var responseData = learningRoleService.UpdateMutiple(requestData); if (responseData) { var datas = learningRoleService.GetLearningRolesByCircleKey(requestData.CircleKey.ToLower(), requestData.Token); if (datas.FirstOrDefault() != null) { response.Data = datas; } response.Success = true; response.Message = "修改成功"; } else { response.Message = "修改失敗"; response.State = Infrastructure.ViewModel.Base.LogState.Error; } return(Ok(response)); }
/// <summary> /// 建立多筆 /// </summary> /// <param name="requestData"></param> /// <returns></returns> public IEnumerable <LearningRole> CreateMutiple(LearningRolePostResquest requestData) { var db = _uow.DbContext; var tokenService = new TokenService(); var tokenInfo = tokenService.GetTokenInfo(requestData.Token).Result; if (tokenInfo == null) { return(null); } var learningCircleService = new LearningCircleService(); var learningCircleInfo = learningCircleService.GetDetailByOuterKey(requestData.CircleKey.ToLower()); if (learningCircleInfo == null) { return(null); } var responseData = new List <LearningRole>(); foreach (var data in requestData.Roles) { var checkEmpty = db.LearningRole.FirstOrDefault(t => t.Name == data.Name && t.Level == data.Level && t.LearningId == learningCircleInfo.Id); if (checkEmpty != null) { continue; } var entity = new LearningRole() { Created = TimeData.Create(DateTime.UtcNow), Deleted = TimeData.Create(null), Updated = TimeData.Create(null), IsAdminRole = data.Level <= 2 ? true : false, Name = data.Name, Level = data.Level, Enable = true, IsFixed = true, LearningId = learningCircleInfo.Id, CreateUser = tokenInfo.MemberId }; db.LearningRole.Add(entity); responseData.Add(entity); } db.SaveChanges(); return(responseData); }