コード例 #1
0
        /// <summary>Lấy thông tin nhân viên theo id</summary>
        /// <param name="Id">The identifier.</param>
        /// <returns>
        ///   <br />
        /// </returns>
        /// <exception cref="ThainhLabExeption">Không tìm thấy nhân viên có Id: {Id}</exception>
        /// <Modified>
        /// Name     Date         Comments
        /// thainh2  5/10/2021   created
        /// </Modified>
        public async Task <StaffViewModel> GetById(int Id)
        {
            try
            {
                var staff = await _context.Staffs.FindAsync(Id);

                if (staff == null)
                {
                    throw new ThainhLabExeption($"Không tìm thấy nhân viên có Id: {Id}");
                }
                var staffVm = new StaffViewModel()
                {
                    Id    = staff.Id,
                    Name  = staff.Name,
                    Email = staff.Email,
                    Tel   = staff.Tel
                };

                return(staffVm);
            }
            catch (Exception e)
            {
                var logRequest = new LogWritelogRequest()
                {
                    MethodName  = "StaffService_GetById",
                    CreateDate  = DateTime.Now,
                    Description = e.Message
                };
                await WriteLog(logRequest);

                throw new ThainhLabExeption(e.Message);
            }
        }
コード例 #2
0
        /// <summary>Cập nhật trạng thái nhân viên</summary>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <br />
        /// </returns>
        /// <exception cref="ThainhLabExeption"></exception>
        /// <Modified>
        /// Name     Date         Comments
        /// thainh2  5/10/2021   created
        /// </Modified>
        public async Task <ApiResult <string> > UpdateStatus(StaffUpdateStatusRequest request)
        {
            try
            {
                var staff = await _context.Staffs.FindAsync(request.Id);

                if (staff == null)
                {
                    string _mess = $"Không tìm thấy nhân viên có Id: {request.Id}";
                    return(new ApiErrorResult <string>(_mess));

                    throw new ThainhLabExeption(_mess);
                }

                staff.UpdateDate = DateTime.Now;
                staff.UpdateBy   = request.UpdateBy;
                staff.Status     = request.Status;
                var result = await _context.SaveChangesAsync();

                return(new ApiSuccessResult <string>(result.ToString()));
            }
            catch (Exception e)
            {
                var logRequest = new LogWritelogRequest()
                {
                    MethodName  = "StaffService_UpdateStatus",
                    CreateDate  = DateTime.Now,
                    Description = e.Message
                };
                await WriteLog(logRequest);

                throw new ThainhLabExeption(e.Message);
            }
        }
コード例 #3
0
        /// <summary>Xóa nhân viên</summary>
        /// <param name="Id">The identifier.</param>
        /// <returns>
        ///   <br />
        /// </returns>
        /// <exception cref="ThainhLabExeption"></exception>
        /// <Modified>
        /// Name     Date         Comments
        /// thainh2  5/10/2021   created
        /// </Modified>
        public async Task <ApiResult <string> > Delete(int Id)
        {
            try
            {
                var staff = await _context.Staffs.FindAsync(Id);

                if (staff == null)
                {
                    string _mes = $"Không tìm thấy nhân viên có Id: {Id}";
                    return(new ApiErrorResult <string>(_mes));

                    throw new ThainhLabExeption(_mes);
                }
                _context.Remove(staff);
                var result = await _context.SaveChangesAsync();

                return(new ApiSuccessResult <string>(result.ToString()));
            }
            catch (Exception e)
            {
                var logRequest = new LogWritelogRequest()
                {
                    MethodName  = "StaffService_Delete",
                    CreateDate  = DateTime.Now,
                    Description = e.Message
                };
                await WriteLog(logRequest);

                throw new ThainhLabExeption(e.Message);
            }
        }
コード例 #4
0
        /// <summary>Tạo mới nhân viên</summary>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <br />
        /// </returns>
        /// <Modified>
        /// Name     Date         Comments
        /// thainh2  5/10/2021   created
        /// </Modified>
        public async Task <ApiResult <string> > Create(StaffCreateRequest request)
        {
            try
            {
                // Check trùng email
                var staffObj = await _context.Staffs.FirstOrDefaultAsync(t => t.Email == request.Email.Trim() && t.Status == true);

                if (staffObj != null)
                {
                    return(new ApiErrorResult <string>("Email đã tồn tại"));
                }

                //Check trùng số điện thoại
                var objStaff_PhoneNumber = await _context.Staffs.FirstOrDefaultAsync(t => t.Tel == request.Tel.Trim() && t.Status == true);

                if (objStaff_PhoneNumber != null)
                {
                    return(new ApiErrorResult <string>("Số điện thoại đã tồn tại"));
                }

                var staff = new Staff()
                {
                    Name         = SystemConstants.StripHTML(request.Name),
                    NameUnsigned = SystemConstants.ConvertToUnSign(SystemConstants.StripHTML(request.Name)),
                    Email        = request.Email,
                    Tel          = request.Tel,
                    Status       = true,
                    CreateBy     = request.CreateBy,
                    CreateDate   = DateTime.Now
                };
                _context.Staffs.Add(staff);
                await _context.SaveChangesAsync();

                return(new ApiSuccessResult <string>(staff.Id.ToString()));
            }
            catch (Exception e)
            {
                var logRequest = new LogWritelogRequest()
                {
                    MethodName  = "StaffService_Create",
                    CreateDate  = DateTime.Now,
                    Description = e.Message
                };
                await WriteLog(logRequest);

                throw new ThainhLabExeption(e.Message);
            }
        }
コード例 #5
0
        /// <summary>Cập nhật thông tin nhân viên</summary>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <br />
        /// </returns>
        /// <exception cref="ThainhLabExeption">Không tìm thấy nhân viên có Id: {request.Id}</exception>
        /// <Modified>
        /// Name     Date         Comments
        /// thainh2  5/10/2021   created
        /// </Modified>
        public async Task <ApiResult <string> > Update(StaffUpdateRequest request)
        {
            try
            {
                var staff = await _context.Staffs.FindAsync(request.Id);

                if (staff == null)
                {
                    throw new ThainhLabExeption($"Không tìm thấy nhân viên có Id: {request.Id}");
                }
                // Check trùng email
                var staffObj = await _context.Staffs.FirstOrDefaultAsync(t => t.Email == request.Email.Trim() && t.Status == true && t.Id != request.Id);

                if (staffObj != null)
                {
                    return(new ApiErrorResult <string>("Email đã tồn tại"));
                }
                // Check trùng số điện thoại
                var objStaff_PhoneNumber = await _context.Staffs.FirstOrDefaultAsync(t => t.Tel == request.Tel.Trim() && t.Status == true && t.Id != request.Id);

                if (objStaff_PhoneNumber != null)
                {
                    return(new ApiErrorResult <string>("Số điện thoại đã tồn tại"));
                }

                staff.Name         = SystemConstants.StripHTML(request.Name);
                staff.NameUnsigned = SystemConstants.ConvertToUnSign(SystemConstants.StripHTML(request.Name));
                staff.Email        = request.Email;
                staff.Tel          = request.Tel;
                staff.UpdateBy     = request.UpdateBy;
                staff.UpdateDate   = DateTime.Now;
                var result = await _context.SaveChangesAsync();

                return(new ApiSuccessResult <string>(result.ToString()));
            }
            catch (Exception e)
            {
                var logRequest = new LogWritelogRequest()
                {
                    MethodName  = "StaffService_Update",
                    CreateDate  = DateTime.Now,
                    Description = e.Message
                };
                await WriteLog(logRequest);

                throw new ThainhLabExeption(e.Message);
            }
        }
コード例 #6
0
        /// <summary>
        ///   <para>
        /// Lấy danh sách phân trang nhân viên</para>
        ///   <para>
        ///     <br />
        ///   </para>
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <br />
        /// </returns>
        /// <Modified>
        /// Name     Date         Comments
        /// thainh2  5/10/2021   created
        /// </Modified>
        public async Task <PagedResult <StaffViewModel> > GetAllPaging(GetStaffPagingRequest request)
        {
            try
            {
                // 1.Select
                var query = from s in _context.Staffs
                            .Where(t => t.Status == true)
                            select new { s };
                //2. Filter
                if (!string.IsNullOrEmpty(request.Keyword))
                {
                    query = query.Where(t => t.s.Name.Contains(request.Keyword) || t.s.NameUnsigned.Contains(request.Keyword));
                }
                // 3. Paging
                int totalRecords = await query.CountAsync();

                var data = await query.OrderByDescending(t => t.s.CreateDate).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize)
                           .Select(x => new StaffViewModel()
                {
                    Id    = x.s.Id,
                    Name  = x.s.Name,
                    Email = x.s.Email,
                    Tel   = x.s.Tel
                }).ToListAsync();

                //4. Select and projection
                var pagedResult = new PagedResult <StaffViewModel>()
                {
                    TotalRecords = totalRecords,
                    PageIndex    = request.PageIndex,
                    PageSize     = request.PageSize,
                    Items        = data
                };
                return(pagedResult);
            }
            catch (Exception e)
            {
                var logRequest = new LogWritelogRequest()
                {
                    MethodName  = "StaffService_GetAllPaging",
                    CreateDate  = DateTime.Now,
                    Description = e.Message
                };
                await WriteLog(logRequest);

                throw new ThainhLabExeption(e.Message);
            }
        }
コード例 #7
0
 /// <summary>Ghi log vào database</summary>
 /// <param name="request">The request.</param>
 /// <returns>
 ///   <br />
 /// </returns>
 /// <Modified>
 /// Name     Date         Comments
 /// thainh2  5/10/2021   created
 /// </Modified>
 public async Task <int> WriteLog(LogWritelogRequest request)
 {
     try
     {
         var log = new Log()
         {
             MethodName  = request.MethodName,
             Description = request.Description,
             CreateDate  = request.CreateDate
         };
         _context.Logs.Add(log);
         return(await _context.SaveChangesAsync());
     }
     catch (Exception e)
     {
         throw new ThainhLabExeption(e.Message);
     }
 }
コード例 #8
0
        /// <summary>Lấy phân trang danh sách nhân viên theo cách store procedure</summary>
        /// <param name="request">The request.</param>
        /// <returns>
        ///   <br />
        /// </returns>
        /// <exception cref="NotImplementedException"></exception>
        /// <Modified>
        /// Name     Date         Comments
        /// thainh2  5/10/2021   created
        /// </Modified>
        public async Task <PagedResult <StaffViewModel> > GetAllPagingStoreProcedure(GetStaffPagingRequest request)
        {
            try
            {
                var pars = new SqlParameter[4];
                pars[0] = new SqlParameter("@PageIndex", request.PageIndex);
                pars[1] = new SqlParameter("@PageSize", request.PageSize);
                pars[2] = new SqlParameter("@Keyword", !string.IsNullOrEmpty(request.Keyword) ? request.Keyword : "");
                pars[3] = new SqlParameter("@TotalRecords", SqlDbType.Int)
                {
                    Direction = ParameterDirection.Output
                };

                var data = await _context.StaffViewModels.FromSqlRaw("dbo.Staff_GetAllPaging @PageIndex,@PageSize,@Keyword,@TotalRecords OUTPUT  ", pars).ToListAsync();

                int totalRecords = int.Parse(pars[3].Value.ToString());
                var pagedResult  = new PagedResult <StaffViewModel>()
                {
                    TotalRecords = totalRecords,
                    PageIndex    = request.PageIndex,
                    PageSize     = request.PageSize,
                    Items        = data
                };
                return(pagedResult);
            }
            catch (Exception e)
            {
                var logRequest = new LogWritelogRequest()
                {
                    MethodName  = "StaffService_GetAllPagingByStoreProcedure",
                    CreateDate  = DateTime.Now,
                    Description = e.Message
                };
                await WriteLog(logRequest);

                throw new ThainhLabExeption(e.Message);
            }
        }