public async Task <ResponseEntity> RegisterUser(UserJiraModel modelVm) { try { UserJira entity = await _useJiraRepository.GetSingleByConditionAsync("email", modelVm.email); if (entity != null) // Kiểm tra email đã được sử dụng bởi tài khoản khác chưa { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, modelVm, MessageConstants.EMAIL_EXITST)); } entity = _mapper.Map <UserJira>(modelVm); entity.avatar = "https://ui-avatars.com/api/?name=" + entity.name; entity.alias = FuncUtilities.BestLower(modelVm.name); entity.deleted = false; //entity.gender = ; //entity.Id = Guid.NewGuid().ToString(); // Mã hóa mật khẩu //entity.MatKhau = BCrypt.Net.BCrypt.HashPassword(modelVm.MatKhau); //entity.avatar = "/static/user-icon.png"; entity = await _useJiraRepository.InsertAsync(entity); if (entity == null) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, modelVm, MessageConstants.SIGNUP_ERROR)); } return(new ResponseEntity(StatusCodeConstants.OK, modelVm, MessageConstants.SIGNUP_SUCCESS)); } catch (Exception ex) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, modelVm, MessageConstants.SIGNUP_ERROR)); } }
public async Task <ResponseEntity> removeUSerFromProject(UserProject project, string token) { UserJira user = _userService.getUserByToken(token).Result; Project pro = _projectRepository.GetSingleByConditionAsync("id", project.projectId).Result; if (pro == null) { return(new ResponseEntity(StatusCodeConstants.NOT_FOUND, "Project is not found!", MessageConstants.MESSAGE_ERROR_404)); } if (pro.creator != user.id) { return(new ResponseEntity(StatusCodeConstants.FORBIDDEN, "User is unthorization!", MessageConstants.MESSAGE_ERROR_403)); } List <KeyValuePair <string, dynamic> > columns = new List <KeyValuePair <string, dynamic> >(); columns.Add(new KeyValuePair <string, dynamic>("projectId", project.projectId)); columns.Add(new KeyValuePair <string, dynamic>("userId", project.userId)); IEnumerable <Project_User> lstProjectUser = _projectUserRepository.GetMultiByListConditionAndAsync(columns).Result; List <dynamic> lstId = new List <dynamic>(); foreach (var item in lstProjectUser) { lstId.Add(item.id); } await _projectUserRepository.DeleteByIdAsync(lstId); return(new ResponseEntity(StatusCodeConstants.OK, "remove user from project successfully !", MessageConstants.MESSAGE_SUCCESS_200)); }
public async Task <ResponseEntity> updateEstimate(updateEstimate model, string token) { var task = _taskRepository.GetSingleByConditionAsync("taskId", model.taskId).Result; if (task == null) { return(new ResponseEntity(StatusCodeConstants.NOT_FOUND, "task is not found!", MessageConstants.MESSAGE_ERROR_404)); } UserJira user = _userService.getUserByToken(token).Result; List <KeyValuePair <string, dynamic> > columns = new List <KeyValuePair <string, dynamic> >(); columns.Add(new KeyValuePair <string, dynamic>("taskId", task.taskId)); columns.Add(new KeyValuePair <string, dynamic>("userId", user.id)); var projectUser = _taskUserRepository.GetMultiByListConditionAndAsync(columns).Result; if (projectUser.Count() == 0) { return(new ResponseEntity(StatusCodeConstants.NOT_FOUND, "user is not assign!", MessageConstants.MESSAGE_ERROR_404)); } task.originalEstimate = model.originalEstimate; await _taskRepository.UpdateAsync("taskId", task.taskId, task); return(new ResponseEntity(StatusCodeConstants.OK, "Update task successfully!", MessageConstants.UPDATE_SUCCESS)); }
public async Task <ResponseEntity> updateStatusTask(UpdateStatusVM statusTask, string token) { UserJira user = _userService.getUserByToken(token).Result; var task = _taskRepository.GetSingleByConditionAsync("taskId", statusTask.taskId).Result; List <KeyValuePair <string, dynamic> > columns = new List <KeyValuePair <string, dynamic> >(); columns.Add(new KeyValuePair <string, dynamic>("taskId", task.taskId)); //columns.Add(new KeyValuePair<string, dynamic>("userId", user.id)); if (task == null) { return(new ResponseEntity(StatusCodeConstants.NOT_FOUND, "task is not found!", MessageConstants.MESSAGE_ERROR_404)); } var projectUser = _taskUserRepository.GetMultiByListConditionAndAsync(columns).Result; //if(projectUser.Count() == 0) //{ // return new ResponseEntity(StatusCodeConstants.NOT_FOUND, "user is not assign!", MessageConstants.MESSAGE_ERROR_404); //} task.statusId = statusTask.statusId; await _taskRepository.UpdateAsync("taskId", task.taskId, task); return(new ResponseEntity(StatusCodeConstants.OK, "Update task successfully!", MessageConstants.MESSAGE_SUCCESS_200)); }
public async Task <ResponseEntity> addUserProject(UserProject project, string token = "") { UserJira user = _userService.getUserByToken(token).Result; Project pro = _projectRepository.GetSingleByConditionAsync("id", project.projectId).Result; if (pro == null) { return(new ResponseEntity(StatusCodeConstants.NOT_FOUND, "Project is not found!", MessageConstants.MESSAGE_ERROR_404)); } if (pro.creator != user.id) { return(new ResponseEntity(StatusCodeConstants.FORBIDDEN, "User is unthorization!", MessageConstants.MESSAGE_ERROR_403)); } List <KeyValuePair <string, dynamic> > columns = new List <KeyValuePair <string, dynamic> >(); columns.Add(new KeyValuePair <string, dynamic>("projectId", project.projectId)); columns.Add(new KeyValuePair <string, dynamic>("userId", project.userId)); IEnumerable <Project_User> lstProjectUser = _projectUserRepository.GetMultiByListConditionAndAsync(columns).Result; if (lstProjectUser.Count() > 0) { return(new ResponseEntity(StatusCodeConstants.ERROR_SERVER, "User already exists in the project!", MessageConstants.MESSAGE_ERROR_500)); } Project_User model = new Project_User(); model.userId = project.userId; model.projectId = project.projectId; model.deleted = false; model.alias = project.userId + "_" + project.projectId; await _projectUserRepository.InsertAsync(model); return(new ResponseEntity(StatusCodeConstants.OK, "has added the user to the project !", MessageConstants.MESSAGE_SUCCESS_200)); }
public async Task <ResponseEntity> removeTask(int taskId, string token) { var task = _taskRepository.GetSingleByConditionAsync("taskId", taskId).Result; if (task == null) { return(new ResponseEntity(StatusCodeConstants.NOT_FOUND, "task is not found!", MessageConstants.MESSAGE_ERROR_404)); } UserJira user = _userService.getUserByToken(token).Result; Project pro = _projectRepository.GetSingleByConditionAsync("id", task.projectId).Result; if (pro == null) { return(new ResponseEntity(StatusCodeConstants.NOT_FOUND, "Project is not found!", MessageConstants.MESSAGE_ERROR_404)); } if (pro.creator != user.id) { return(new ResponseEntity(StatusCodeConstants.FORBIDDEN, "User is unthorization!", MessageConstants.MESSAGE_ERROR_403)); } dynamic taskIDD = taskId; IEnumerable <Task_User> taskUser = await _taskUserRepository.GetMultiByConditionAsync("taskId", taskIDD).Result; List <dynamic> lstIdTaskUser = new List <dynamic>(); //Xóa task user foreach (var taskU in taskUser) { lstIdTaskUser.Add(taskU.id); } await _taskUserRepository.DeleteByIdAsync(lstIdTaskUser); //Xóa task comment dynamic taskCommnetId = taskId; IEnumerable <Comment> comment = await _userComment.GetMultiByConditionAsync("taskId", taskCommnetId).Result; List <dynamic> lstIdComment = new List <dynamic>(); foreach (var item in comment) { lstIdComment.Add(item.id); } await _userComment.DeleteByIdAsync(lstIdComment); //Xóa task List <dynamic> lst = new List <dynamic>(); lst.Add(task.taskId); await _taskRepository.DeleteByIdAsync(lst); return(new ResponseEntity(StatusCodeConstants.OK, "Remove task successfully!", MessageConstants.MESSAGE_SUCCESS_200)); }
public async Task <ResponseEntity> createTask(taskInsert model, string token) { UserJira user = _userService.getUserByToken(token).Result; Project pro = _projectRepository.GetSingleByConditionAsync("id", model.projectId).Result; if (pro == null) { return(new ResponseEntity(StatusCodeConstants.NOT_FOUND, "Project is not found!", MessageConstants.MESSAGE_ERROR_404)); } if (pro.creator != user.id) { return(new ResponseEntity(StatusCodeConstants.FORBIDDEN, "User is unthorization!", MessageConstants.MESSAGE_ERROR_403)); } string alias = FuncUtilities.BestLower(model.taskName); //Kiểm tra task tồn tại chưa var taskValid = _taskRepository.GetSingleByConditionAsync("alias", alias).Result; if (taskValid != null) { return(new ResponseEntity(StatusCodeConstants.ERROR_SERVER, "task already exists!", MessageConstants.MESSAGE_ERROR_500)); } Repository.Models.Task task = new Repository.Models.Task(); task.taskName = model.taskName; task.alias = FuncUtilities.BestLower(model.taskName); task.description = FuncUtilities.Base64Encode(model.description); task.statusId = model.statusId; task.originalEstimate = model.originalEstimate; task.timeTrackingSpent = model.timeTrackingSpent; task.timeTrackingRemaining = model.timeTrackingRemaining; task.projectId = model.projectId; task.typeId = model.typeId; task.reporterId = user.id; task.priorityId = model.priorityId; task.deleted = false; task.reporterId = user.id; task = _taskRepository.InsertAsync(task).Result; foreach (var item in model.listUserAsign) { Task_User tu = new Task_User(); tu.taskId = task.taskId; tu.deleted = false; tu.userId = item; await _taskUserRepository.InsertAsync(tu); } return(new ResponseEntity(StatusCodeConstants.OK, task, "create task successfully!")); }
public async Task <UserJira> getUserByToken(string tokenString) { try { string email = FuncUtilities.parseJWTToEmail(tokenString); UserJira us = await _useJiraRepository.GetSingleByConditionAsync("email", email); return(us); } catch (Exception ex) { return(null); } }
public List <ViewModels.ProjectViewModel.Member> getListMember(int projectId) { List <ViewModels.ProjectViewModel.Member> lst = new List <ViewModels.ProjectViewModel.Member>(); var userProject = _projectUserRepository.GetMultiByConditionAsync("projectId", projectId).Result; foreach (var project in userProject) { ViewModels.ProjectViewModel.Member mem = new ViewModels.ProjectViewModel.Member(); mem.userId = project.userId; dynamic id = mem.userId; UserJira user = _userJira.GetSingleByIdAsync(id).Result; mem.userId = user.id; mem.name = user.name; mem.avatar = user.avatar; lst.Add(mem); } return(lst); }
private async Task <string> GenerateTokenJira(UserJira entity) { try { //UserType group = await _userTypeRepository.GetSingleByIdAsync(entity.userTypeId); //if (group == null) // return string.Empty; //IEnumerable<UserType_Role> group_Role = await _userType_RoleRepository.GetMultiByConditionAsync("userTypeId", group.id); //List<string> lstDanhSachQuyen = new List<string>(); //foreach (var item in group_Role) //{ // lstDanhSachQuyen.Add(item.roleId); //} //string danhSachQuyen = JsonConvert.SerializeObject(lstDanhSachQuyen); //List<string> roles = JsonConvert.DeserializeObject<List<string>>(danhSachQuyen); List <Claim> claims = new List <Claim>(); //claims.Add(new Claim(ClaimTypes.Name, entity.Id)); claims.Add(new Claim(ClaimTypes.Email, entity.email)); //if (roles != null) //{ // foreach (var item in roles) // { // claims.Add(new Claim(ClaimTypes.Role, item.Trim())); // } //} var secret = Encoding.ASCII.GetBytes(_appSettings.Secret); var token = new JwtSecurityToken( claims: claims, notBefore: new DateTimeOffset(DateTime.Now).DateTime, expires: new DateTimeOffset(DateTime.Now.AddMinutes(60)).DateTime, signingCredentials: new SigningCredentials(new SymmetricSecurityKey(secret), SecurityAlgorithms.HmacSha256Signature) ); return(new JwtSecurityTokenHandler().WriteToken(token)); } catch (Exception ex) { throw ex; } }
public async Task <ResponseEntity> addTaskUser(TaskUser model, string token) { var task = _taskRepository.GetSingleByConditionAsync("taskId", model.taskId).Result; if (task == null) { return(new ResponseEntity(StatusCodeConstants.NOT_FOUND, "task is not found!", MessageConstants.MESSAGE_ERROR_404)); } UserJira user = _userService.getUserByToken(token).Result; Project pro = _projectRepository.GetSingleByConditionAsync("id", task.projectId).Result; if (pro == null) { return(new ResponseEntity(StatusCodeConstants.NOT_FOUND, "Project is not found!", MessageConstants.MESSAGE_ERROR_404)); } if (pro.creator != user.id) { return(new ResponseEntity(StatusCodeConstants.FORBIDDEN, "User is unthorization!", MessageConstants.MESSAGE_ERROR_403)); } List <KeyValuePair <string, dynamic> > columns = new List <KeyValuePair <string, dynamic> >(); columns.Add(new KeyValuePair <string, dynamic>("taskId", model.taskId)); columns.Add(new KeyValuePair <string, dynamic>("userId", model.userId)); var taskUser = _taskUserRepository.GetMultiByListConditionAndAsync(columns).Result; if (taskUser.Count() > 0) { return(new ResponseEntity(StatusCodeConstants.ERROR_SERVER, "User is unthorization!", MessageConstants.ACCOUNT_EXITST_TASK)); } Task_User taskUserInsert = new Task_User(); taskUserInsert.userId = model.taskId; taskUserInsert.taskId = model.userId; await _taskUserRepository.InsertAsync(taskUserInsert); return(new ResponseEntity(StatusCodeConstants.OK, "add user to task successfully!", MessageConstants.UPDATE_SUCCESS)); }
public async Task <ResponseEntity> SignIn(UserJiraLogin modelVm) { UserJira entity = await _useJiraRepository.GetSingleByConditionAsync("email", modelVm.email); if (entity == null) // Kiểm tra email đã được sử dụng bởi tài khoản khác chưa { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, "Email", "Email không tồn tại !")); } if (entity.passWord != modelVm.passWord) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, modelVm, "Tài khoản hoặc mật khẩu không đúng !")); } UserJiraModelView usModel = new UserJiraModelView(); usModel.id = entity.id; usModel.name = entity.name; usModel.avatar = entity.avatar; usModel.email = entity.email; usModel.phoneNumber = entity.phoneNumber; usModel.accessToken = await GenerateTokenJira(entity); return(new ResponseEntity(StatusCodeConstants.OK, usModel, MessageConstants.MESSAGE_SUCCESS_200)); }
public async Task <ResponseEntity> SignInFacebookAsync(DangNhapFacebookViewModel modelVm) { string[] ERR_MESSAGE = { "Vui lòng nhập email bạn đã đăng ký!", "Email này đã được sử dụng cho tài khoản facebook khác!", "Email không chính xác!" }; string[] ERR_STATUS = { "EMAIL_ENTER", "EMAIL_EXISTS", "EMAIL_INCORRECT" }; try { var httpClient = new HttpClient { BaseAddress = new Uri("https://graph.facebook.com/v2.9/") }; var response = await httpClient.GetAsync($"me?access_token={modelVm.facebookToken}&fields=id,name,email,first_name,last_name,age_range,birthday,gender,locale"); if (!response.IsSuccessStatusCode) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, "Login facebook failure !", "Please, try login again")); } var result = await response.Content.ReadAsStringAsync(); dynamic facebookAccount = JsonConvert.DeserializeObject <FacebookResult>(result); //Checkfacebook id UserJira facebookUser = await _useJiraRepository.GetSingleByConditionAsync("facebookId", facebookAccount.id); if (facebookUser != null) { UserLoginResult userResult = new UserLoginResult(); userResult.email = facebookUser.email; userResult.accessToken = await GenerateTokenJira(facebookUser); return(new ResponseEntity(StatusCodeConstants.OK, userResult, MessageConstants.SIGNIN_SUCCESS)); } //Đăng nhập thành công fb kiểm tra có email không nếu có cho dn thành công Type objType = facebookAccount.GetType(); if (objType.GetProperty("email") != null) { //Kiểm tra có email chưa lấy ra AppUser userCheckEmail = await _userRepository.GetSingleByConditionAsync("email", facebookAccount.email); if (userCheckEmail != null) { //Cập nhật fb id cho mail đó userCheckEmail.facebookId = facebookAccount.id; await _userRepository.UpdateByConditionAsync("email", facebookAccount.email, userCheckEmail); UserLoginResult userResult = new UserLoginResult(); userResult.email = userCheckEmail.email; userResult.accessToken = await GenerateTokenJira(facebookUser); return(new ResponseEntity(StatusCodeConstants.OK, userResult, MessageConstants.SIGNIN_SUCCESS)); } } //Nếu chưa có tạo tài khoản AppUser userModel = new AppUser(); userModel.facebookId = facebookAccount.id; userModel.name = facebookAccount.first_name + " " + facebookAccount.last_name; userModel.email = userModel.facebookId + "@facebook.com"; userModel.deleted = false; userModel.avatar = "/static/user-icon.png"; userModel.userTypeId = "CUSTOMER"; AppUser userInsert = await _userRepository.InsertAsync(userModel); if (userInsert != null) { UserLoginResult userResult = new UserLoginResult(); userResult.email = userModel.email; userResult.accessToken = await GenerateToken(userModel); return(new ResponseEntity(StatusCodeConstants.OK, userResult, MessageConstants.SIGNIN_SUCCESS)); } return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ERR_STATUS[0], ERR_MESSAGE[0])); } catch (Exception ex) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ERR_STATUS[0], ERR_MESSAGE[0])); } //register if required //var facebookUser = _context.FacebookUsers.SingleOrDefault(x => x.Id == facebookAccount.Id); //if (facebookUser == null) //{ // var user = new ApplicationUser { UserName = facebookAccount.Name, Email = facebookAccount.Email }; // var result2 = await _userManager.CreateAsync(user); // if (!result2.Succeeded) return BadRequest(); // facebookUser = new FacebookUser { Id = facebookAccount.Id, UserId = user.Id }; // _context.FacebookUsers.Add(facebookUser); // _context.SaveChanges(); //} // } // return new ResponseEntity(StatusCodeConstants.OK, result, MessageConstants.SIGNIN_SUCCESS); //} //catch (Exception ex) //{ // return new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ex.Message, MessageConstants.SIGNIN_ERROR); //} //string[] ERR_MESSAGE = { "Vui lòng nhập email bạn đã đăng ký!", "Email này đã được sử dụng cho tài khoản facebook khác!", "Email không chính xác!" }; //string[] ERR_STATUS = { "EMAIL_ENTER", "EMAIL_EXISTS", "EMAIL_INCORRECT" }; //try //{ // UserLoginResult result = new UserLoginResult(); // AppUser entity = await _userRepository.GetByFacebookAsync(modelVm.FacebookId); // if (entity != null) // Nếu FacebookId đúng => đăng nhập thành công // { // // Tạo token // result.accessToken = await GenerateToken(entity); // result.email = entity.email; // return new ResponseEntity(StatusCodeConstants.OK, result, MessageConstants.SIGNIN_SUCCESS); // } // //// Nếu facebook id sai và email chưa nhập // //if (string.IsNullOrEmpty(modelVm.Email)) // // return new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ERR_STATUS[0], ERR_MESSAGE[0]); // if (entity == null) // { // var httpClient = new HttpClient { BaseAddress = new Uri("https://graph.facebook.com/v2.9/") }; // var response = await httpClient.GetAsync($"me?access_token={facebookToken.Token}&fields=id,name,email,first_name,last_name,age_range,birthday,gender,locale,picture"); // if (!response.IsSuccessStatusCode) return BadRequest(); // var result = await response.Content.ReadAsStringAsync(); // var facebookAccount = JsonConvert.DeserializeObject<FacebookAccount>(result); // //register if required // var facebookUser = _context.FacebookUsers.SingleOrDefault(x => x.Id == facebookAccount.Id); // if (facebookUser == null) // { // var user = new ApplicationUser { UserName = facebookAccount.Name, Email = facebookAccount.Email }; // var result2 = await _userManager.CreateAsync(user); // if (!result2.Succeeded) return BadRequest(); // facebookUser = new FacebookUser { Id = facebookAccount.Id, UserId = user.Id }; // _context.FacebookUsers.Add(facebookUser); // _context.SaveChanges(); // } // } // return new ResponseEntity(StatusCodeConstants.OK, result, MessageConstants.SIGNIN_SUCCESS); //} //catch (Exception ex) //{ // return new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ex.Message, MessageConstants.SIGNIN_ERROR); //} }
public async Task <ResponseEntity> updateTask(TaskEdit model, string token) { UserJira user = _userService.getUserByToken(token).Result; Project pro = _projectRepository.GetSingleByConditionAsync("id", model.projectId).Result; Repository.Models.Task taskModel = _taskRepository.GetSingleByConditionAsync("taskId", model.taskId).Result; if (taskModel == null) { return(new ResponseEntity(StatusCodeConstants.NOT_FOUND, "Task is not found!", MessageConstants.MESSAGE_ERROR_404)); } if (pro == null) { return(new ResponseEntity(StatusCodeConstants.NOT_FOUND, "Project is not found!", MessageConstants.MESSAGE_ERROR_404)); } if (pro.creator != user.id) { return(new ResponseEntity(StatusCodeConstants.FORBIDDEN, "User is unthorization!", MessageConstants.MESSAGE_ERROR_403)); } string alias = FuncUtilities.BestLower(model.taskName); //Kiểm tra task tồn tại chưa var taskValid = _taskRepository.GetSingleByConditionAsync("alias", alias).Result; if (taskValid.taskId != taskModel.taskId) { return(new ResponseEntity(StatusCodeConstants.ERROR_SERVER, "Task name already exists!", MessageConstants.MESSAGE_ERROR_500)); } taskModel.taskName = model.taskName; taskModel.alias = FuncUtilities.BestLower(model.taskName); taskModel.description = FuncUtilities.Base64Encode(model.description); taskModel.statusId = model.statusId; taskModel.originalEstimate = model.originalEstimate; taskModel.timeTrackingSpent = model.timeTrackingSpent; taskModel.timeTrackingRemaining = model.timeTrackingRemaining; taskModel.projectId = model.projectId; taskModel.typeId = model.typeId; taskModel.reporterId = user.id; taskModel.priorityId = model.priorityId; taskModel.deleted = false; await _taskRepository.UpdateAsync("taskId", taskModel.taskId, taskModel); //dell user cũ var taskUserCu = _taskUserRepository.GetMultiByConditionAsync("taskId", taskModel.taskId).Result; if (taskUserCu.Count() > 0) { List <dynamic> lstDynamicId = new List <dynamic>(); foreach (var item in taskUserCu) { lstDynamicId.Add(item.id); } await _taskUserRepository.DeleteByIdAsync(lstDynamicId); } foreach (var item in model.listUserAsign) { Task_User tu = new Task_User(); tu.taskId = taskModel.taskId; tu.deleted = false; tu.userId = item; await _taskUserRepository.InsertAsync(tu); } return(new ResponseEntity(StatusCodeConstants.OK, taskModel, "update task successfully!")); }