瀏覽代碼

attachment upload json response

zinab_elgendy 2 月之前
父節點
當前提交
fd6c231106

+ 2 - 2
MTWorkHR.API/Controllers/AttachmentController.cs

@@ -25,9 +25,9 @@ namespace MTWorkHR.API.Controllers
         [HttpPost("Upload")]
         [ProducesResponseType(StatusCodes.Status200OK)]
         [Consumes("multipart/form-data")]
-        public async Task<ActionResult<string>> Upload(IFormFile input)
+        public async Task<ActionResult<AttachmentResponseDto>> Upload(IFormFile input)
         {
-            return await _fileService.UploadFile(input);
+            return Ok( await _fileService.UploadFile(input));
         }
 
 

+ 1 - 8
MTWorkHR.API/Controllers/ContractController.cs

@@ -73,14 +73,7 @@ namespace MTWorkHR.API.Controllers
             return await _ContractService.ChangeStatus(input.ContractId, input.StatusId);
         }
 
-        [HttpPost("UploadAttachment")]
-        [ProducesResponseType(StatusCodes.Status200OK)]
-        [Consumes("multipart/form-data")]
-        public async Task<ActionResult<string>> UploadAttachment(IFormFile input)
-        {
-            return await _fileService.UploadFile(input);
-        }
-
+       
         //[HttpPost("CreateTaskAttachment")]
         //[ProducesResponseType(StatusCodes.Status200OK)]
         //[Consumes("multipart/form-data")]

+ 15 - 0
MTWorkHR.Application/Dtos/Identity/AttachmentResponseDto.cs

@@ -0,0 +1,15 @@
+using Microsoft.AspNetCore.Http;
+using MTWorkHR.Core.Entities.Base;
+using System.ComponentModel.DataAnnotations;
+using System.ComponentModel.DataAnnotations.Schema;
+
+namespace MTWorkHR.Application.Models
+{
+    public class AttachmentResponseDto 
+    {
+        public string? FileName { get; set; }
+
+        public string? FilePath { get; set; }
+
+    }
+}

+ 3 - 0
MTWorkHR.Application/Dtos/User/CompanyDto.cs

@@ -20,6 +20,9 @@ namespace MTWorkHR.Application.Models
         public string? Email { get; set; }
         public long? CountryId { get; set; }
         public long? CityId { get; set; }
+        public string? CityName { get; set; }
+        public string? PostalCode { get; set; }
+        public string? CountryName { get; set; }
         public UserTypeEnum? UserType { get; set; }
         public CompanyUserDto? CompanyUser { get; set; }
         public IFormFile? ProfileImage { get; set; }

+ 3 - 1
MTWorkHR.Application/Mapper/MappingProfile.cs

@@ -114,7 +114,9 @@ namespace MTWorkHR.Application.Mapper
             CreateMap<OrderType, OrderTypeDto>().ReverseMap();
             CreateMap<LeaveType, LeaveTypeDto>().ReverseMap();
             CreateMap<CompanyDto, Company>().ForMember(d => d.CreateDate, o => o.Ignore()).ForMember(d => d.CreateUser, o => o.Ignore());
-            CreateMap<Company, CompanyDto>();
+            CreateMap<Company, CompanyDto>()
+                .ForMember(s => s.CityName, o => o.MapFrom(s => s.City == null ? "" : GlobalInfo.lang == "ar" ? s.City.NameAr : s.City.NameEn))
+                .ForMember(s => s.CountryName, o => o.MapFrom(s => s.Country == null ? "" : GlobalInfo.lang == "ar" ? s.Country.NameAr : s.Country.NameEn));
             CreateMap<CountryLookup, CountryDto>().ReverseMap();
             CreateMap<Industry, IndustryDto>().ReverseMap();
             CreateMap<JobTitle, JobTitleDto>().ReverseMap();

+ 9 - 6
MTWorkHR.Application/Services/Base/BlobFileService.cs

@@ -27,8 +27,9 @@ namespace MTWorkHR.Application.Services
             _containerClient = _blobServiceClient.GetBlobContainerClient(ContainerName);
             _containerClient.CreateIfNotExists();
         }
-        public async Task<string> UploadFile(IFormFile file)
+        public async Task<AttachmentResponseDto> UploadFile(IFormFile file)
         {
+            AttachmentResponseDto result = new AttachmentResponseDto();
             try
             {
                 var blobClient = _containerClient.GetBlobClient(file.FileName);
@@ -36,13 +37,15 @@ namespace MTWorkHR.Application.Services
                 {
                     var status = await blobClient.UploadAsync(file.OpenReadStream(), true);
                     //if(status.Value == "201")
-                    return blobClient.Uri.AbsoluteUri;
+                    result.FileName = file.FileName;
+                    result.FilePath = blobClient.Uri.AbsoluteUri;
+                    return result;
                 }else
-                    return "";
+                    return result;
             }
             catch (Exception ex)
             {
-                return "";
+                return result;
             }
         }
         public async Task<string> UploadFileCloud(AttachmentDto file)
@@ -105,9 +108,9 @@ namespace MTWorkHR.Application.Services
                 return null;
             }
         }
-        public async Task<List<string>> UploadFiles(List<IFormFile> files)
+        public async Task<List<AttachmentResponseDto>> UploadFiles(List<IFormFile> files)
         {
-            List<string> msgs = new List<string>();
+            List<AttachmentResponseDto> msgs = new List<AttachmentResponseDto>();
             foreach (var formFile in files)
             {
                 msgs.Add(await UploadFile(formFile));

+ 5 - 5
MTWorkHR.Application/Services/Base/FileService.cs

@@ -16,13 +16,13 @@ namespace MTWorkHR.Application.Services
         {
             this.settings = settings;
         }
-        public async Task<string> UploadFile(IFormFile file)
+        public async Task<AttachmentResponseDto> UploadFile(IFormFile file)
         {
             var filesName = await UploadFiles(new List<IFormFile> { file });
-            return filesName.First();
+            return  filesName.First() ;
         }
 
-        public async Task< List<string>> UploadFiles(List<IFormFile> files)
+        public async Task< List<AttachmentResponseDto>> UploadFiles(List<IFormFile> files)
         {
             if (!AttachmentsMust(files))
                 throw new AppException(ExceptionEnum.InvalidFileType);
@@ -31,14 +31,14 @@ namespace MTWorkHR.Application.Services
             if (!Directory.Exists(pathToSave))
                 Directory.CreateDirectory(pathToSave);
 
-            var fileNames = new List<string>();
+            var fileNames = new List<AttachmentResponseDto>();
             foreach (var formFile in files)
             {
                 var fname = ContentDispositionHeaderValue.Parse(formFile.ContentDisposition).FileName.Trim('"');
                 var fnameSplit = fname.Split(".");
                 var fNewName = Guid.NewGuid().ToString() + "." + fnameSplit[fnameSplit.Length - 1];
                 var fullPath = Path.Combine(pathToSave, fNewName);
-                fileNames.Add(fNewName);
+                fileNames.Add(new AttachmentResponseDto {FileName = fNewName, FilePath = fullPath });
                 if (formFile.Length > 0)
                 {
                     using (var stream = new FileStream(fullPath, FileMode.Create))

+ 2 - 2
MTWorkHR.Application/Services/Interfaces/IFileService.cs

@@ -10,8 +10,8 @@ namespace MTWorkHR.Application.Services.Interfaces
 {
     public interface IFileService
     {
-        Task<string> UploadFile(IFormFile file);
-        Task<List<string>> UploadFiles(List<IFormFile> files);
+        Task<AttachmentResponseDto> UploadFile(IFormFile file);
+        Task<List<AttachmentResponseDto>> UploadFiles(List<IFormFile> files);
         Task<bool> CopyFileToActualFolder(List<AttachmentDto> attachments);
         bool CopyFileToActualFolder(string FileName);
         bool DeleteFileFromTempFolder(string FileName);

+ 12 - 3
MTWorkHR.Application/Services/User/CompanyService.cs

@@ -43,13 +43,21 @@ namespace MTWorkHR.Application.Services
             _roleManager = roleManager;
         }
 
-     
+       
+
         public async Task<CompanyDto> GetById()
         {
-            var companyResponse = new CompanyDto();
             if (_globalInfo.CompanyId.HasValue)
+                return await GetById(_globalInfo.CompanyId.Value);
+            else 
+                return new CompanyDto();
+        }
+        public override async Task<CompanyDto> GetById(long companyId)
+        {
+            var companyResponse = new CompanyDto();
+            if (companyId > 0)
             {
-                var entity = await _unitOfWork.Company.GetByIdAsync(_globalInfo.CompanyId.Value);
+                var entity = await _unitOfWork.Company.GetByIdWithAllChildren(companyId);
                 companyResponse = MapperObject.Mapper.Map<CompanyDto>(entity);
                 var userDto = await _userService.GetById(entity.UserId);
                 companyResponse.CommercialRegAttach = userDto.CommercialRegAttach;
@@ -108,6 +116,7 @@ namespace MTWorkHR.Application.Services
             input.Address = string.IsNullOrEmpty(input.Address) ? input.CompanyUser?.UserAddress?.AddressDesc: input.Address;
             input.CountryId = input.CountryId!=null && input.CountryId > 0 ?  input.CountryId : input.CompanyUser?.UserAddress?.CountryId;
             input.CityId = input.CityId != null && input.CityId > 0 ?  input.CityId : input.CompanyUser?.UserAddress?.CityId;
+            input.PostalCode = input.PostalCode != null ?  input.PostalCode : input.CompanyUser?.UserAddress?.PostalCode;
             //UserDto companyUser = new UserDto
             //{
             //    UserType = UserTypeEnum.Business,

+ 1 - 0
MTWorkHR.Core/Entities/User/Company.cs

@@ -19,6 +19,7 @@ namespace MTWorkHR.Core.Entities
         public string PhoneNumber { get; set; }
         public string Address { get; set; }
         public string Email { get; set; }
+        public string? PostalCode { get; set; }
         public bool IsSuspended { get; set; }
 
         public long? CountryId { get; set; }

+ 1 - 0
MTWorkHR.Core/IRepositories/Auth/ICompanyRepository.cs

@@ -11,5 +11,6 @@ namespace MTWorkHR.Core.IRepositories
 {
     public interface ICompanyRepository : IRepository<Company>
     {
+        Task<Company> GetByIdWithAllChildren(long id);
     }
 }

+ 12 - 1
MTWorkHR.Infrastructure/Repositories/User/CompanyRepository.cs

@@ -9,9 +9,20 @@ namespace MTWorkHR.Infrastructure.Repositories
 {
     public class CompanyRepository : Repository<Company>, ICompanyRepository
     {
+        private readonly DbSet<Company> dbSet;
+   
         public CompanyRepository(HRDataContext context) : base(context)
         {
-        }
+            dbSet = context.Set<Company>();
 
+          
+        }
+        public async Task<Company> GetByIdWithAllChildren(long id)
+        {
+            return await dbSet
+                .Include(x => x.City)
+                .Include(x => x.Country)
+                .FirstOrDefaultAsync(x => x.Id == id);
+        }
     }
 }