feature/upload_pdf naar BE_dev 3 jaren geleden
| using Diligent.WebAPI.Contracts.DTOs.Categories; | |||||
| using Diligent.WebAPI.Contracts.DTOs.File; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Business.MappingProfiles | |||||
| { | |||||
| public class CategoryMappingProfile : Profile | |||||
| { | |||||
| public CategoryMappingProfile() | |||||
| { | |||||
| #region DTO to Model | |||||
| CreateMap<Category, CategoryResponse>(); | |||||
| CreateMap<Category, CategoriesNamesResponse>(); | |||||
| #endregion | |||||
| #region Model to DTO | |||||
| #endregion | |||||
| } | |||||
| } | |||||
| } |
| using Diligent.WebAPI.Contracts.DTOs.File; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Business.MappingProfiles | |||||
| { | |||||
| public class FileMappingProfile : Profile | |||||
| { | |||||
| public FileMappingProfile() | |||||
| { | |||||
| #region DTO to Model | |||||
| CreateMap<FileEntity, FileEntityResponse>(); | |||||
| #endregion | |||||
| #region Model to DTO | |||||
| #endregion | |||||
| } | |||||
| } | |||||
| } |
| using Diligent.WebAPI.Contracts.DTOs.Categories; | |||||
| using Diligent.WebAPI.Contracts.DTOs.Tags; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Business.MappingProfiles | |||||
| { | |||||
| public class TagMappingProfile : Profile | |||||
| { | |||||
| public TagMappingProfile() | |||||
| { | |||||
| #region DTO to Model | |||||
| CreateMap<Tag, TagResponse>(); | |||||
| CreateMap<Tag, TagNameResponse>(); | |||||
| #endregion | |||||
| #region Model to DTO | |||||
| #endregion | |||||
| } | |||||
| } | |||||
| } |
| using Diligent.WebAPI.Contracts.DTOs.Categories; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Business.Services | |||||
| { | |||||
| public class CategoryService : ICategoryService | |||||
| { | |||||
| private readonly DatabaseContext _context; | |||||
| private readonly IMapper _mapper; | |||||
| public CategoryService(DatabaseContext context, IMapper mapper) | |||||
| { | |||||
| _context = context; | |||||
| _mapper = mapper; | |||||
| } | |||||
| public async Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync() => | |||||
| _mapper.Map<List<CategoriesNamesResponse>>(await _context.Categories.ToListAsync()); | |||||
| public async Task<Category> GetCategoryEntityById(int id) => | |||||
| await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync(); | |||||
| } | |||||
| } |
| using Azure.Core; | |||||
| using Diligent.WebAPI.Contracts.DTOs.File; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Business.Services | |||||
| { | |||||
| public class FileEntityService : IFileEntityService | |||||
| { | |||||
| private readonly ILogger<FileEntityService> _logger; | |||||
| private readonly DatabaseContext _context; | |||||
| private readonly IMapper _mapper; | |||||
| public FileEntityService(DatabaseContext context, ILogger<FileEntityService> logger, IMapper mapper) | |||||
| { | |||||
| _context = context; | |||||
| _logger = logger; | |||||
| _mapper = mapper; | |||||
| } | |||||
| public async Task<List<FileEntityResponse>> GetAllAsync() => | |||||
| _mapper.Map<List<FileEntityResponse>>(await _context.Files.Include(x => x.Tags).ToListAsync()); | |||||
| public async Task UploadPdfAsync(FileEntity file) | |||||
| { | |||||
| await _context.Files.AddAsync(file); | |||||
| await _context.SaveChangesAsync(); | |||||
| } | |||||
| public async Task<IEnumerable<FileEntity>> GetAll() | |||||
| { | |||||
| return await _context.Files.ToListAsync(); | |||||
| } | |||||
| } | |||||
| } |
| string base64 = Convert.ToBase64String(bytes); | string base64 = Convert.ToBase64String(bytes); | ||||
| return base64; | return base64; | ||||
| } | } | ||||
| public async Task<IEnumerable<FileEntity>> GetAll() | |||||
| { | |||||
| return await _context.Files.ToListAsync(); | |||||
| } | |||||
| } | } | ||||
| } | } |
| using Diligent.WebAPI.Contracts.DTOs.Categories; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Business.Services.Interfaces | |||||
| { | |||||
| public interface ICategoryService | |||||
| { | |||||
| Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync(); | |||||
| Task<Category> GetCategoryEntityById(int id); | |||||
| } | |||||
| } |
| using Diligent.WebAPI.Contracts.DTOs.File; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Business.Services.Interfaces | |||||
| { | |||||
| public interface IFileEntityService | |||||
| { | |||||
| Task<List<FileEntityResponse>> GetAllAsync(); | |||||
| Task UploadPdfAsync(FileEntity file); | |||||
| Task<IEnumerable<FileEntity>> GetAll(); | |||||
| } | |||||
| } |
| { | { | ||||
| Task<string> GetCV(string fileName); | Task<string> GetCV(string fileName); | ||||
| Task UploadCV(string fileName,IFormFile file); | Task UploadCV(string fileName,IFormFile file); | ||||
| Task<IEnumerable<FileEntity>> GetAll(); | |||||
| } | } | ||||
| } | } |
| | | ||||
| using Diligent.WebAPI.Contracts.DTOs.Files; | using Diligent.WebAPI.Contracts.DTOs.Files; | ||||
| using Diligent.WebAPI.Contracts.DTOs.Tags; | |||||
| namespace Diligent.WebAPI.Business.Services.Interfaces | namespace Diligent.WebAPI.Business.Services.Interfaces | ||||
| { | { | ||||
| public interface ITagService | public interface ITagService | ||||
| { | { | ||||
| Task<FileFiltersReturnDTO> GetFilters(); | Task<FileFiltersReturnDTO> GetFilters(); | ||||
| Task<List<TagNameResponse>> GetTagsNames(); | |||||
| Task<Tag> GetTagEntityById(int id); | |||||
| } | } | ||||
| } | } |
| using Diligent.WebAPI.Contracts.DTOs.Files; | |||||
| using Diligent.WebAPI.Contracts.DTOs.Categories; | |||||
| using Diligent.WebAPI.Contracts.DTOs.Files; | |||||
| using Diligent.WebAPI.Contracts.DTOs.Tags; | |||||
| using Microsoft.EntityFrameworkCore; | |||||
| namespace Diligent.WebAPI.Business.Services | namespace Diligent.WebAPI.Business.Services | ||||
| { | { | ||||
| public class TagService : ITagService | public class TagService : ITagService | ||||
| { | { | ||||
| private readonly DatabaseContext _databaseContext; | private readonly DatabaseContext _databaseContext; | ||||
| private readonly IMapper _mapper; | |||||
| public TagService(DatabaseContext databaseContext) | |||||
| public TagService(DatabaseContext databaseContext, IMapper mapper) | |||||
| { | { | ||||
| _databaseContext = databaseContext; | _databaseContext = databaseContext; | ||||
| _mapper = mapper; | |||||
| } | } | ||||
| public async Task<FileFiltersReturnDTO> GetFilters() | public async Task<FileFiltersReturnDTO> GetFilters() | ||||
| Extensions = extensions | Extensions = extensions | ||||
| }; | }; | ||||
| } | } | ||||
| public async Task<List<TagNameResponse>> GetTagsNames() => | |||||
| _mapper.Map<List<TagNameResponse>>(await _databaseContext.Tags.ToListAsync()); | |||||
| public async Task<Tag> GetTagEntityById(int id) => | |||||
| await _databaseContext.Tags.Where(x => x.Id == id).FirstOrDefaultAsync(); | |||||
| } | } | ||||
| } | } |
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Contracts.DTOs.Categories | |||||
| { | |||||
| public class CategoriesNamesResponse | |||||
| { | |||||
| public int Id { get; set; } | |||||
| public string Name { get; set; } | |||||
| } | |||||
| } |
| using Diligent.WebAPI.Contracts.DTOs.File; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.ComponentModel.DataAnnotations.Schema; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Contracts.DTOs.Categories | |||||
| { | |||||
| public class CategoryResponse | |||||
| { | |||||
| public int Id { get; set; } | |||||
| public string Name { get; set; } | |||||
| public List<FileEntityResponse> Files { get; set; } | |||||
| } | |||||
| } |
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Contracts.DTOs.File | |||||
| { | |||||
| public class CreateFileDto | |||||
| { | |||||
| public int CategoryId { get; set; } | |||||
| public int[] TagsIds { get; set; } | |||||
| public string UploadedFile { get; set; } | |||||
| } | |||||
| } |
| using Microsoft.AspNetCore.Http; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Contracts.DTOs.File | |||||
| { | |||||
| public class CreateFileRequest | |||||
| { | |||||
| public int CategoryId { get; set; } | |||||
| public int[] TagsIds { get; set; } | |||||
| public IFormFile FileToUpload { get; set; } | |||||
| } | |||||
| } |
| using Diligent.WebAPI.Contracts.DTOs.Categories; | |||||
| using Diligent.WebAPI.Contracts.DTOs.Tags; | |||||
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Contracts.DTOs.File | |||||
| { | |||||
| public class FileEntityResponse | |||||
| { | |||||
| public int Id { get; set; } | |||||
| public string Name { get; set; } | |||||
| public long Size { get; set; } | |||||
| public string Extension { get; set; } | |||||
| public List<TagResponse> Tags { get; set; } | |||||
| } | |||||
| } |
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Contracts.DTOs.Tags | |||||
| { | |||||
| public class TagNameResponse | |||||
| { | |||||
| public int Id { get; set; } | |||||
| public string Name { get; set; } | |||||
| } | |||||
| } |
| using System; | |||||
| using System.Collections.Generic; | |||||
| using System.Linq; | |||||
| using System.Text; | |||||
| using System.Threading.Tasks; | |||||
| namespace Diligent.WebAPI.Contracts.DTOs.Tags | |||||
| { | |||||
| public class TagResponse | |||||
| { | |||||
| public int Id { get; set; } | |||||
| public string Name { get; set; } | |||||
| } | |||||
| } |
| public string Name { get; set; } | public string Name { get; set; } | ||||
| [ForeignKey(nameof(File))] | |||||
| public int FileId { get; set; } | |||||
| public FileEntity File { get; set; } | |||||
| public List<FileEntity> Files { get; set; } | |||||
| } | } | ||||
| } | } |
| using System; | using System; | ||||
| using System.Collections.Generic; | using System.Collections.Generic; | ||||
| using System.ComponentModel.DataAnnotations.Schema; | |||||
| using System.Linq; | using System.Linq; | ||||
| using System.Text; | using System.Text; | ||||
| using System.Threading.Tasks; | using System.Threading.Tasks; | ||||
| public string Extension { get; set; } | public string Extension { get; set; } | ||||
| public List<Category> Categories { get; set; } | |||||
| [ForeignKey(nameof(Category))] | |||||
| public int CategoryId { get; set; } | |||||
| public Category Category { get; set; } | |||||
| public List<Tag> Tags { get; set; } | public List<Tag> Tags { get; set; } | ||||
| } | } |
| namespace Diligent.WebAPI.Data.Migrations | namespace Diligent.WebAPI.Data.Migrations | ||||
| { | { | ||||
| [DbContext(typeof(DatabaseContext))] | [DbContext(typeof(DatabaseContext))] | ||||
| [Migration("20230203064003_AddedFileEntity")] | |||||
| partial class AddedFileEntity | |||||
| [Migration("20230203145727_AddedFileAndCategoryAndTagEntities")] | |||||
| partial class AddedFileAndCategoryAndTagEntities | |||||
| { | { | ||||
| protected override void BuildTargetModel(ModelBuilder modelBuilder) | protected override void BuildTargetModel(ModelBuilder modelBuilder) | ||||
| { | { | ||||
| SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1); | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1); | ||||
| b.Property<int>("FileId") | |||||
| .HasColumnType("int"); | |||||
| b.Property<string>("Name") | b.Property<string>("Name") | ||||
| .IsRequired() | .IsRequired() | ||||
| .HasColumnType("nvarchar(max)"); | .HasColumnType("nvarchar(max)"); | ||||
| b.HasKey("Id"); | b.HasKey("Id"); | ||||
| b.HasIndex("FileId"); | |||||
| b.ToTable("Categories"); | b.ToTable("Categories"); | ||||
| }); | }); | ||||
| SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1); | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1); | ||||
| b.Property<int>("CategoryId") | |||||
| .HasColumnType("int"); | |||||
| b.Property<string>("Extension") | b.Property<string>("Extension") | ||||
| .IsRequired() | .IsRequired() | ||||
| .HasColumnType("nvarchar(max)"); | .HasColumnType("nvarchar(max)"); | ||||
| b.HasKey("Id"); | b.HasKey("Id"); | ||||
| b.HasIndex("CategoryId"); | |||||
| b.ToTable("Files"); | b.ToTable("Files"); | ||||
| }); | }); | ||||
| .IsRequired(); | .IsRequired(); | ||||
| }); | }); | ||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.Category", b => | |||||
| { | |||||
| b.HasOne("Diligent.WebAPI.Data.Entities.FileEntity", "File") | |||||
| .WithMany("Categories") | |||||
| .HasForeignKey("FileId") | |||||
| .OnDelete(DeleteBehavior.Cascade) | |||||
| .IsRequired(); | |||||
| b.Navigation("File"); | |||||
| }); | |||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.Comment", b => | modelBuilder.Entity("Diligent.WebAPI.Data.Entities.Comment", b => | ||||
| { | { | ||||
| b.HasOne("Diligent.WebAPI.Data.Entities.Applicant", "Applicant") | b.HasOne("Diligent.WebAPI.Data.Entities.Applicant", "Applicant") | ||||
| b.Navigation("User"); | b.Navigation("User"); | ||||
| }); | }); | ||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.FileEntity", b => | |||||
| { | |||||
| b.HasOne("Diligent.WebAPI.Data.Entities.Category", "Category") | |||||
| .WithMany("Files") | |||||
| .HasForeignKey("CategoryId") | |||||
| .OnDelete(DeleteBehavior.Cascade) | |||||
| .IsRequired(); | |||||
| b.Navigation("Category"); | |||||
| }); | |||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.InsurancePolicy", b => | modelBuilder.Entity("Diligent.WebAPI.Data.Entities.InsurancePolicy", b => | ||||
| { | { | ||||
| b.HasOne("Diligent.WebAPI.Data.Entities.Insurer", "Insurer") | b.HasOne("Diligent.WebAPI.Data.Entities.Insurer", "Insurer") | ||||
| b.Navigation("TechnologyApplicants"); | b.Navigation("TechnologyApplicants"); | ||||
| }); | }); | ||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.FileEntity", b => | |||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.Category", b => | |||||
| { | { | ||||
| b.Navigation("Categories"); | |||||
| b.Navigation("Files"); | |||||
| }); | }); | ||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.SelectionLevel", b => | modelBuilder.Entity("Diligent.WebAPI.Data.Entities.SelectionLevel", b => |
| namespace Diligent.WebAPI.Data.Migrations | namespace Diligent.WebAPI.Data.Migrations | ||||
| { | { | ||||
| public partial class AddedFileEntity : Migration | |||||
| public partial class AddedFileAndCategoryAndTagEntities : Migration | |||||
| { | { | ||||
| protected override void Up(MigrationBuilder migrationBuilder) | protected override void Up(MigrationBuilder migrationBuilder) | ||||
| { | { | ||||
| migrationBuilder.CreateTable( | migrationBuilder.CreateTable( | ||||
| name: "Files", | |||||
| name: "Categories", | |||||
| columns: table => new | columns: table => new | ||||
| { | { | ||||
| Id = table.Column<int>(type: "int", nullable: false) | Id = table.Column<int>(type: "int", nullable: false) | ||||
| .Annotation("SqlServer:Identity", "1, 1"), | .Annotation("SqlServer:Identity", "1, 1"), | ||||
| Name = table.Column<string>(type: "nvarchar(max)", nullable: false), | |||||
| Size = table.Column<long>(type: "bigint", nullable: false), | |||||
| Extension = table.Column<string>(type: "nvarchar(max)", nullable: false) | |||||
| Name = table.Column<string>(type: "nvarchar(max)", nullable: false) | |||||
| }, | }, | ||||
| constraints: table => | constraints: table => | ||||
| { | { | ||||
| table.PrimaryKey("PK_Files", x => x.Id); | |||||
| table.PrimaryKey("PK_Categories", x => x.Id); | |||||
| }); | }); | ||||
| migrationBuilder.CreateTable( | migrationBuilder.CreateTable( | ||||
| }); | }); | ||||
| migrationBuilder.CreateTable( | migrationBuilder.CreateTable( | ||||
| name: "Categories", | |||||
| name: "Files", | |||||
| columns: table => new | columns: table => new | ||||
| { | { | ||||
| Id = table.Column<int>(type: "int", nullable: false) | Id = table.Column<int>(type: "int", nullable: false) | ||||
| .Annotation("SqlServer:Identity", "1, 1"), | .Annotation("SqlServer:Identity", "1, 1"), | ||||
| Name = table.Column<string>(type: "nvarchar(max)", nullable: false), | Name = table.Column<string>(type: "nvarchar(max)", nullable: false), | ||||
| FileId = table.Column<int>(type: "int", nullable: false) | |||||
| Size = table.Column<long>(type: "bigint", nullable: false), | |||||
| Extension = table.Column<string>(type: "nvarchar(max)", nullable: false), | |||||
| CategoryId = table.Column<int>(type: "int", nullable: false) | |||||
| }, | }, | ||||
| constraints: table => | constraints: table => | ||||
| { | { | ||||
| table.PrimaryKey("PK_Categories", x => x.Id); | |||||
| table.PrimaryKey("PK_Files", x => x.Id); | |||||
| table.ForeignKey( | table.ForeignKey( | ||||
| name: "FK_Categories_Files_FileId", | |||||
| column: x => x.FileId, | |||||
| principalTable: "Files", | |||||
| name: "FK_Files_Categories_CategoryId", | |||||
| column: x => x.CategoryId, | |||||
| principalTable: "Categories", | |||||
| principalColumn: "Id", | principalColumn: "Id", | ||||
| onDelete: ReferentialAction.Cascade); | onDelete: ReferentialAction.Cascade); | ||||
| }); | }); | ||||
| onDelete: ReferentialAction.Cascade); | onDelete: ReferentialAction.Cascade); | ||||
| }); | }); | ||||
| migrationBuilder.CreateIndex( | |||||
| name: "IX_Categories_FileId", | |||||
| table: "Categories", | |||||
| column: "FileId"); | |||||
| migrationBuilder.CreateIndex( | migrationBuilder.CreateIndex( | ||||
| name: "IX_FileEntityTag_TagsId", | name: "IX_FileEntityTag_TagsId", | ||||
| table: "FileEntityTag", | table: "FileEntityTag", | ||||
| column: "TagsId"); | column: "TagsId"); | ||||
| migrationBuilder.CreateIndex( | |||||
| name: "IX_Files_CategoryId", | |||||
| table: "Files", | |||||
| column: "CategoryId"); | |||||
| } | } | ||||
| protected override void Down(MigrationBuilder migrationBuilder) | protected override void Down(MigrationBuilder migrationBuilder) | ||||
| { | { | ||||
| migrationBuilder.DropTable( | |||||
| name: "Categories"); | |||||
| migrationBuilder.DropTable( | migrationBuilder.DropTable( | ||||
| name: "FileEntityTag"); | name: "FileEntityTag"); | ||||
| migrationBuilder.DropTable( | migrationBuilder.DropTable( | ||||
| name: "Tags"); | name: "Tags"); | ||||
| migrationBuilder.DropTable( | |||||
| name: "Categories"); | |||||
| } | } | ||||
| } | } | ||||
| } | } |
| SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1); | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1); | ||||
| b.Property<int>("FileId") | |||||
| .HasColumnType("int"); | |||||
| b.Property<string>("Name") | b.Property<string>("Name") | ||||
| .IsRequired() | .IsRequired() | ||||
| .HasColumnType("nvarchar(max)"); | .HasColumnType("nvarchar(max)"); | ||||
| b.HasKey("Id"); | b.HasKey("Id"); | ||||
| b.HasIndex("FileId"); | |||||
| b.ToTable("Categories"); | b.ToTable("Categories"); | ||||
| }); | }); | ||||
| SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1); | SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1); | ||||
| b.Property<int>("CategoryId") | |||||
| .HasColumnType("int"); | |||||
| b.Property<string>("Extension") | b.Property<string>("Extension") | ||||
| .IsRequired() | .IsRequired() | ||||
| .HasColumnType("nvarchar(max)"); | .HasColumnType("nvarchar(max)"); | ||||
| b.HasKey("Id"); | b.HasKey("Id"); | ||||
| b.HasIndex("CategoryId"); | |||||
| b.ToTable("Files"); | b.ToTable("Files"); | ||||
| }); | }); | ||||
| .IsRequired(); | .IsRequired(); | ||||
| }); | }); | ||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.Category", b => | |||||
| { | |||||
| b.HasOne("Diligent.WebAPI.Data.Entities.FileEntity", "File") | |||||
| .WithMany("Categories") | |||||
| .HasForeignKey("FileId") | |||||
| .OnDelete(DeleteBehavior.Cascade) | |||||
| .IsRequired(); | |||||
| b.Navigation("File"); | |||||
| }); | |||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.Comment", b => | modelBuilder.Entity("Diligent.WebAPI.Data.Entities.Comment", b => | ||||
| { | { | ||||
| b.HasOne("Diligent.WebAPI.Data.Entities.Applicant", "Applicant") | b.HasOne("Diligent.WebAPI.Data.Entities.Applicant", "Applicant") | ||||
| b.Navigation("User"); | b.Navigation("User"); | ||||
| }); | }); | ||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.FileEntity", b => | |||||
| { | |||||
| b.HasOne("Diligent.WebAPI.Data.Entities.Category", "Category") | |||||
| .WithMany("Files") | |||||
| .HasForeignKey("CategoryId") | |||||
| .OnDelete(DeleteBehavior.Cascade) | |||||
| .IsRequired(); | |||||
| b.Navigation("Category"); | |||||
| }); | |||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.InsurancePolicy", b => | modelBuilder.Entity("Diligent.WebAPI.Data.Entities.InsurancePolicy", b => | ||||
| { | { | ||||
| b.HasOne("Diligent.WebAPI.Data.Entities.Insurer", "Insurer") | b.HasOne("Diligent.WebAPI.Data.Entities.Insurer", "Insurer") | ||||
| b.Navigation("TechnologyApplicants"); | b.Navigation("TechnologyApplicants"); | ||||
| }); | }); | ||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.FileEntity", b => | |||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.Category", b => | |||||
| { | { | ||||
| b.Navigation("Categories"); | |||||
| b.Navigation("Files"); | |||||
| }); | }); | ||||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.SelectionLevel", b => | modelBuilder.Entity("Diligent.WebAPI.Data.Entities.SelectionLevel", b => |
| using Microsoft.AspNetCore.Mvc; | |||||
| namespace Diligent.WebAPI.Host.Controllers.V1 | |||||
| { | |||||
| [ApiVersion("1.0")] | |||||
| [Route("v{version:apiVersion}/categories")] | |||||
| [ApiController] | |||||
| public class CategoriesController : ControllerBase | |||||
| { | |||||
| private readonly ICategoryService _categoryService; | |||||
| public CategoriesController(ICategoryService categoryService) | |||||
| { | |||||
| _categoryService = categoryService; | |||||
| } | |||||
| [HttpGet("names")] | |||||
| public async Task<IActionResult> GetCategoriesNames() => | |||||
| Ok(await _categoryService.GetCategoriesNamesAsync()); | |||||
| } | |||||
| } |
| namespace Diligent.WebAPI.Host.Controllers.V1 | |||||
| using Diligent.WebAPI.Contracts.DTOs.File; | |||||
| using Diligent.WebAPI.Host.Methods; | |||||
| namespace Diligent.WebAPI.Host.Controllers.V1 | |||||
| { | { | ||||
| [ApiVersion("1.0")] | [ApiVersion("1.0")] | ||||
| [Route("v{version:apiVersion}/files")] | [Route("v{version:apiVersion}/files")] | ||||
| [ApiController] | [ApiController] | ||||
| public class FilesController : ControllerBase | public class FilesController : ControllerBase | ||||
| { | { | ||||
| private readonly IFileService _fileService; | |||||
| private readonly ICategoryService _categoryService; | |||||
| private readonly ITagService _tagService; | |||||
| private readonly IFileEntityService _fileEntityService; | |||||
| private readonly Microsoft.AspNetCore.Hosting.IHostingEnvironment _hostingEnvironment; | |||||
| public FilesController(IFileService fileService) | |||||
| public FilesController(Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment, IFileEntityService fileEntityService, ICategoryService categoryService, ITagService tagService) | |||||
| { | { | ||||
| _fileService = fileService; | |||||
| _hostingEnvironment = hostingEnvironment; | |||||
| _fileEntityService = fileEntityService; | |||||
| _categoryService = categoryService; | |||||
| _tagService = tagService; | |||||
| } | } | ||||
| [HttpGet] | [HttpGet] | ||||
| public async Task<IActionResult> GetAll() => Ok(await _fileService.GetAll()); | |||||
| public async Task<IActionResult> GetAll() => Ok(await _fileEntityService.GetAll()); | |||||
| [HttpPost] | |||||
| public async Task<IActionResult> UploadPdf([FromForm] CreateFileRequest request) | |||||
| { | |||||
| if (request == null) throw new BadHttpRequestException("Request cannot be null"); | |||||
| var filePath = await Upload.SaveFile(_hostingEnvironment.ContentRootPath, request.FileToUpload, "files"); | |||||
| var category = await _categoryService.GetCategoryEntityById(request.CategoryId); | |||||
| if (category == null) throw new BadHttpRequestException("Category cannot found"); | |||||
| List<Tag> tags = new(); | |||||
| foreach (var id in request.TagsIds) | |||||
| tags.Add(await _tagService.GetTagEntityById(id)); | |||||
| await _fileEntityService.UploadPdfAsync(new FileEntity { CategoryId = request.CategoryId, Category = category, Tags = tags, Name = filePath, Extension = "pdf", Size = 100}); | |||||
| return Ok(); | |||||
| } | |||||
| } | } | ||||
| } | } |
| namespace Diligent.WebAPI.Host.Controllers.V1 | |||||
| using Diligent.WebAPI.Contracts.DTOs.Tags; | |||||
| namespace Diligent.WebAPI.Host.Controllers.V1 | |||||
| { | { | ||||
| [ApiVersion("1.0")] | [ApiVersion("1.0")] | ||||
| [Route("v{version:apiVersion}/tags")] | [Route("v{version:apiVersion}/tags")] | ||||
| public class TagsController : ControllerBase | public class TagsController : ControllerBase | ||||
| { | { | ||||
| private readonly ITagService _tagService; | private readonly ITagService _tagService; | ||||
| private readonly IMapper _mapper; | |||||
| public TagsController(ITagService tagService) | |||||
| public TagsController(ITagService tagService, IMapper mapper) | |||||
| { | { | ||||
| _tagService = tagService; | _tagService = tagService; | ||||
| _mapper = mapper; | |||||
| } | } | ||||
| [HttpGet] | [HttpGet] | ||||
| public async Task<IActionResult> GetFileFilters() => Ok(await _tagService.GetFilters()); | public async Task<IActionResult> GetFileFilters() => Ok(await _tagService.GetFilters()); | ||||
| [HttpGet("names")] | |||||
| public async Task<IActionResult> GetTagsNames() => Ok(await _tagService.GetTagsNames()); | |||||
| } | } | ||||
| } | } |
| <ItemGroup> | <ItemGroup> | ||||
| <Folder Include="Files\" /> | <Folder Include="Files\" /> | ||||
| <Folder Include="wwwroot\files\" /> | |||||
| </ItemGroup> | </ItemGroup> | ||||
| </Project> | </Project> |
| services.AddScoped<ISaveImportedDataService, SaveImportedDataService>(); | services.AddScoped<ISaveImportedDataService, SaveImportedDataService>(); | ||||
| services.AddScoped<IScreeningTestService, ScreeningTestService>(); | services.AddScoped<IScreeningTestService, ScreeningTestService>(); | ||||
| services.AddScoped<IFileService, FileService>(); | services.AddScoped<IFileService, FileService>(); | ||||
| services.AddScoped<IFileEntityService, FileEntityService>(); | |||||
| services.AddScoped<ITagService, TagService>(); | services.AddScoped<ITagService, TagService>(); | ||||
| services.AddScoped<ICategoryService, CategoryService>(); | |||||
| } | } | ||||
| /// <summary> | /// <summary> |
| namespace Diligent.WebAPI.Host.Methods | |||||
| { | |||||
| public class Upload | |||||
| { | |||||
| public static async Task<string> SaveFile(string root, IFormFile file, string path) | |||||
| { | |||||
| string fileName = new String(Path.GetFileNameWithoutExtension(file.Name).Take(10).ToArray()).Replace(' ', '-'); | |||||
| fileName = fileName + DateTime.Now.ToString("yymmssfff") + Path.GetExtension(file.FileName); | |||||
| var filePath = Path.Combine(root, $"wwwroot/{path}", fileName); | |||||
| using (var fileStream = new FileStream(filePath, FileMode.Create)) | |||||
| { | |||||
| await file.CopyToAsync(fileStream); | |||||
| } | |||||
| return fileName; | |||||
| } | |||||
| } | |||||
| } |