public async Task <IActionResult> UpdateStaff([FromRoute] int Id, [FromForm] StaffUpdateRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            request.Id = Id;
            var result = await _staffService.Update(request);

            if (!result.IsSuccessed)
            {
                return(BadRequest(result));
            }
            return(Ok(result));
        }
Beispiel #2
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);
            }
        }
Beispiel #3
0
        /// <summary>Cập nhật thông tin 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> > UpdateStaff(StaffUpdateRequest request)
        {
            var client = _httpClientFactory.CreateClient();

            client.BaseAddress = new Uri(_configuration[SystemConstants.AppSettings.BaseAddress]);
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _token);

            var requestContent = new MultipartFormDataContent();

            requestContent.Add(new StringContent(string.IsNullOrEmpty(request.Name) ? "" : request.Name.ToString()), "name");
            requestContent.Add(new StringContent(string.IsNullOrEmpty(request.Email) ? "" : request.Email.ToString()), "email");
            requestContent.Add(new StringContent(string.IsNullOrEmpty(request.Tel) ? "" : request.Tel.ToString()), "tel");
            requestContent.Add(new StringContent(string.IsNullOrEmpty(_userId) ? "" : _userId), "updateby");

            var response = await client.PutAsync($"/api/staffs/" + request.Id, requestContent);

            if (response.IsSuccessStatusCode)
            {
                return(JsonConvert.DeserializeObject <ApiSuccessResult <string> >(await response.Content.ReadAsStringAsync()));
            }
            return(JsonConvert.DeserializeObject <ApiErrorResult <string> >(await response.Content.ReadAsStringAsync()));
        }