Exemplo n.º 1
0
        public async Task <ResponseEntity> ChangePasswordAsync(DoiMatKhauViewModel modelVm)
        {
            try
            {
                NguoiDung entity = await _nguoiDungRepository.GetSingleByIdAsync(modelVm.Id);

                if (entity == null)
                {
                    return(new ResponseEntity(StatusCodeConstants.NOT_FOUND));
                }

                entity.MatKhau = BCrypt.Net.BCrypt.HashPassword(modelVm.MatKhau);

                entity = await _nguoiDungRepository.UpdateAsync(entity.Id, entity);

                if (entity == null)
                {
                    return(new ResponseEntity(StatusCodeConstants.BAD_REQUEST, modelVm, MessageConstants.UPDATE_ERROR));
                }

                return(new ResponseEntity(StatusCodeConstants.OK, modelVm, MessageConstants.UPDATE_SUCCESS));
            }
            catch
            {
                return(new ResponseEntity(StatusCodeConstants.ERROR_SERVER));
            }
        }
Exemplo n.º 2
0
        public async Task <ResponseEntity> GetByUserIdAsync(dynamic id)
        {
            try
            {
                NguoiDung nguoiDung = await _nguoiDungRepository.GetSingleByIdAsync(id);

                if (nguoiDung == null || nguoiDung.DanhSachLopHoc == null)
                {
                    return(new ResponseEntity(StatusCodeConstants.NOT_FOUND));
                }

                List <LopHocViewModel> dsLopHocVm = new List <LopHocViewModel>();
                if (nguoiDung.DanhSachLopHoc != null)
                {
                    List <dynamic> dsMaLopHoc = JsonConvert.DeserializeObject <List <dynamic> >(nguoiDung.DanhSachLopHoc);
                    var            dsLopHoc   = await _lopHocRepository.GetMultiByListIdAsync(dsMaLopHoc);

                    dsLopHocVm = _mapper.Map <List <LopHocViewModel> >(dsLopHoc);
                }
                return(new ResponseEntity(StatusCodeConstants.OK, dsLopHocVm));
            }
            catch (Exception ex)
            {
                return(new ResponseEntity(StatusCodeConstants.ERROR_SERVER, ex.Message));
            }
        }
Exemplo n.º 3
0
        public async Task <ResponseEntity> UpdateUserAsync(string id, SuaNguoiDungViewModel modelVm)
        {
            try
            {
                NguoiDung entity = await _nguoiDungRepository.GetSingleByIdAsync(id);

                if (entity == null)
                {
                    return(new ResponseEntity(StatusCodeConstants.NOT_FOUND, modelVm));
                }

                entity = _mapper.Map <NguoiDung>(modelVm);
                await _nguoiDungRepository.UpdateAsync(id, entity);

                return(new ResponseEntity(StatusCodeConstants.OK, modelVm, MessageConstants.UPDATE_SUCCESS));
            }
            catch (Exception ex)
            {
                return(new ResponseEntity(StatusCodeConstants.ERROR_SERVER, ex.Message));
            }
        }
        public override async Task <ResponseEntity> InsertAsync(ChuyenLopViewModel modelVm)
        {
            try
            {
                List <KeyValuePair <string, dynamic> > columns = new List <KeyValuePair <string, dynamic> >();
                columns.Add(new KeyValuePair <string, dynamic>("MaNguoiDung", modelVm.MaNguoiDung));
                columns.Add(new KeyValuePair <string, dynamic>("MaLopChuyenDi", modelVm.MaLopChuyenDi));
                columns.Add(new KeyValuePair <string, dynamic>("MaLopChuyenDen", modelVm.MaLopChuyenDen));

                ChuyenLop entity = await _chuyenLopRepository.GetSingleByListConditionAsync(columns);

                if (entity != null)
                {
                    if (entity.MaLopChuyenDen != modelVm.MaLopChuyenDen && entity.MaLopChuyenDi != modelVm.MaLopChuyenDi)
                    {
                        entity.MaLopChuyenDi  = modelVm.MaLopChuyenDen;
                        entity.MaLopChuyenDen = modelVm.MaLopChuyenDi;
                        await _chuyenLopRepository.InsertAsync(entity);
                    }
                    return(new ResponseEntity(StatusCodeConstants.OK, modelVm, MessageConstants.UPDATE_SUCCESS));
                }

                // XÓA MÃ LỚP HỌC CŨ, THÊM MÃ LỚP HỌC MỚI VÀO DANH SÁCH LỚP HỌC CỦA BẢNG USER
                NguoiDung nguoiDung = await _nguoiDungRepository.GetSingleByIdAsync(modelVm.MaNguoiDung);

                HashSet <int> dsMaLopHoc = JsonConvert.DeserializeObject <HashSet <int> >(nguoiDung.DanhSachLopHoc);
                dsMaLopHoc.RemoveWhere(x => x == modelVm.MaLopChuyenDi);
                dsMaLopHoc.Add(modelVm.MaLopChuyenDen);
                nguoiDung.DanhSachLopHoc = JsonConvert.SerializeObject(dsMaLopHoc);

                // XÓA MÃ NGƯỜI DÙNG CỦA KHỎI LỚP HỌC CHUYỂN ĐI
                LopHoc lopChuyenDi = await _lopHocRepository.GetSingleByIdAsync(modelVm.MaLopChuyenDi);

                HashSet <string> dsMaNguoiDung = JsonConvert.DeserializeObject <HashSet <string> >(lopChuyenDi.DanhSachHocVien);
                dsMaNguoiDung.RemoveWhere(x => x == modelVm.MaNguoiDung);
                lopChuyenDi.DanhSachHocVien = JsonConvert.SerializeObject(dsMaNguoiDung);

                // THÊM MÃ NGƯỜI DÙNG VÀO LỚP HỌC CHUYỂN ĐẾN
                LopHoc lopChuyenDen = await _lopHocRepository.GetSingleByIdAsync(modelVm.MaLopChuyenDen);

                dsMaNguoiDung = JsonConvert.DeserializeObject <HashSet <string> >(lopChuyenDen.DanhSachHocVien);
                dsMaNguoiDung.Add(modelVm.MaNguoiDung);
                lopChuyenDen.DanhSachHocVien = JsonConvert.SerializeObject(dsMaNguoiDung);

                await _nguoiDungRepository.UpdateAsync(nguoiDung.Id, nguoiDung);

                await _lopHocRepository.UpdateAsync(lopChuyenDi.Id, lopChuyenDi);

                await _lopHocRepository.UpdateAsync(lopChuyenDen.Id, lopChuyenDen);

                modelVm.TenLopChuyenDi  = lopChuyenDi.TenLopHoc;
                modelVm.TenLopChuyenDen = lopChuyenDen.TenLopHoc;
                modelVm.NgayChuyen      = DateTime.Now.ToString("dd/MM/yyyy");
                entity = _mapper.Map <ChuyenLop>(modelVm);
                entity = await _chuyenLopRepository.InsertAsync(entity);

                return(new ResponseEntity(StatusCodeConstants.OK, modelVm, MessageConstants.UPDATE_SUCCESS));
            }
            catch (Exception ex)
            {
                return(new ResponseEntity(StatusCodeConstants.ERROR_SERVER, ex.Message));
            }
        }