public async Task <ResponseEntity> SignUpAsync(DangKyViewModel modelVm) { try { NguoiDung entity = await _nguoiDungRepository.GetByEmailAsync(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 <NguoiDung>(modelVm); entity.Id = Guid.NewGuid().ToString(); // Mã hóa mật khẩu entity.MatKhau = BCrypt.Net.BCrypt.HashPassword(modelVm.MatKhau); entity.Avatar = !string.IsNullOrEmpty(modelVm.Avatar) ? modelVm.Avatar : "/static/user-icon.png"; entity.MaNhomQuyen = "HOCVIEN"; entity = await _nguoiDungRepository.InsertAsync(entity); if (entity == null) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, modelVm, MessageConstants.SIGNUP_ERROR)); } NguoiDungViewModel model = _mapper.Map <NguoiDungViewModel>(entity); return(new ResponseEntity(StatusCodeConstants.CREATED, model, MessageConstants.SIGNUP_SUCCESS)); } catch { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, modelVm, MessageConstants.SIGNUP_ERROR)); } }
public async Task <ResponseEntity> RegisterAsync(int id, KhachHangGhiDanhViewModel modelVm) { try { if (await _khachHangRepository.GetSingleByIdAsync(id) == null) { return(new ResponseEntity(StatusCodeConstants.NOT_FOUND)); } DangKyViewModel dangKyModel = new DangKyViewModel() { Email = modelVm.Email, MatKhau = modelVm.MatKhau, HoTen = modelVm.HoTen, BiDanh = modelVm.BiDanh, SoDT = modelVm.SoDT, Avatar = "/static/user-icon.png" }; NguoiDung entity = await _nguoiDungRepository.GetByEmailAsync(modelVm.Email); // Tạo tài khoản cho khách hàng nếu chưa có if (entity == null) { entity = _mapper.Map <NguoiDung>(dangKyModel); entity.Id = Guid.NewGuid().ToString(); // Mã hóa mật khẩu entity.MatKhau = BCrypt.Net.BCrypt.HashPassword(modelVm.MatKhau); entity.MaNhomQuyen = "HOCVIEN"; entity = await _nguoiDungRepository.InsertAsync(entity); } // Lấy ra lớp học có id trùng với mã lớp học truyền lên LopHoc lopHoc = await _lopHocRepository.GetSingleByIdAsync(modelVm.MaLopHoc); //LopHocViewModel lopHocVm = _mapper.Map<LopHocViewModel>(lopHoc); // Thêm vào danh sách HashSet <string> dsHocVien = JsonConvert.DeserializeObject <HashSet <string> >(lopHoc.DanhSachHocVien); dsHocVien.Add(entity.Id); lopHoc.DanhSachHocVien = JsonConvert.SerializeObject(dsHocVien); // Cập nhật lại thông tin lớp await _lopHocRepository.UpdateAsync(lopHoc.Id, lopHoc); return(new ResponseEntity(StatusCodeConstants.CREATED, lopHoc, MessageConstants.SIGNUP_SUCCESS)); } catch (Exception ex) { return(new ResponseEntity(StatusCodeConstants.ERROR_SERVER, ex.Message)); } }
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 { await _lopHocRepository.EnableAsync(); await _lopHocRepository.DisableAsync(); NguoiDung entity = await _nguoiDungRepository.GetByFacebookAsync(modelVm.FacebookId); if (entity != null) // Nếu FacebookId đúng => đăng nhập thành công { // Tạo token entity.Token = await GenerateToken(entity); NguoiDungViewModel model = _mapper.Map <NguoiDungViewModel>(entity); return(new ResponseEntity(StatusCodeConstants.OK, model, 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])); } // Lấy ra thông tin người dùng từ database dựa vào email entity = await _nguoiDungRepository.GetByEmailAsync(modelVm.Email); if (entity == null) { // Kiểm tra xem email đã tồn tại trong bảng khách hàng chưa // - Nếu chưa có thông báo đăng nhập thất bại // - Nếu có thì tạo tài khoản cho user=> đăng nhập thành công KhachHang khachHang = await _khachHangRepository.GetByEmailAsync(modelVm.Email); if (khachHang == null) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ERR_STATUS[2], ERR_MESSAGE[2])); } ThongTinKHViewModel thongTinKHVm = JsonConvert.DeserializeObject <ThongTinKHViewModel>(khachHang.ThongTinKH); // Tạo tài khoản mới cho user entity = new NguoiDung(); entity.Id = Guid.NewGuid().ToString(); entity.Email = thongTinKHVm.Email; entity.MatKhau = BCrypt.Net.BCrypt.HashPassword("Cybersoft@123"); entity.HoTen = khachHang.TenKH; entity.BiDanh = khachHang.BiDanh; entity.SoDT = thongTinKHVm.SoDienThoai; entity.Avatar = "/static/user-icon.png"; entity.MaNhomQuyen = "HOCVIEN"; // Thực hiện truy vấn thêm mới entity = await _nguoiDungRepository.InsertAsync(entity); if (entity == null) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, modelVm, MessageConstants.SIGNIN_ERROR)); } } // Email đúng, FacebookId có tồn tại nhưng không khớp với facebook id đang đăng nhập // Cái này để tránh trường hợp 1 email xài cho nhiều tài khoản else if (!string.IsNullOrEmpty(entity.FacebookId)) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ERR_STATUS[1], ERR_MESSAGE[1])); } // Lưu FacebookId vào database entity.FacebookId = modelVm.FacebookId; entity = await _nguoiDungRepository.UpdateAsync(entity.Id, entity); // Tạo token entity.Token = await GenerateToken(entity); NguoiDungViewModel result = _mapper.Map <NguoiDungViewModel>(entity); return(new ResponseEntity(StatusCodeConstants.OK, result, MessageConstants.SIGNIN_SUCCESS)); } catch (Exception ex) { return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, ex.Message, MessageConstants.SIGNIN_ERROR)); } }