| @@ -13,7 +13,7 @@ namespace Diligent.WebAPI.Business.Extensions | |||
| } | |||
| public static List<FileFilterReturnDto> FilterByCategory(this List<FileFilterReturnDto> query, string[]? values) | |||
| { | |||
| if (values == null || values.Length == 0) | |||
| if (values == null || values.Length == 0 || values[0] == "null") | |||
| return query; | |||
| return query.Where(n => values.Contains(n.Category.CategoryName)).ToList(); | |||
| } | |||
| @@ -60,7 +60,7 @@ namespace Diligent.WebAPI.Business.Services | |||
| return GetCategoriesFromUserCategories(userCategories); | |||
| } | |||
| public async Task<Category> GetCategoryEntityById(int id) => | |||
| public async Task<Category> GetCategoryEntityById(int? id) => | |||
| await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync(); | |||
| public async Task<List<IsGrantedCategory>> GetCategories(int userId) | |||
| @@ -11,7 +11,7 @@ namespace Diligent.WebAPI.Business.Services.Interfaces | |||
| { | |||
| Task<List<CategoriesNamesResponse>> GetRootCategories(int userId); | |||
| Task<List<CategoriesNamesResponse>> GetChildCategories(int parentCategoryId,int userId); | |||
| Task<Category> GetCategoryEntityById(int id); | |||
| Task<Category> GetCategoryEntityById(int? id); | |||
| Task<List<IsGrantedCategory>> GetCategories(int userId); | |||
| } | |||
| } | |||
| @@ -11,11 +11,10 @@ namespace Diligent.WebAPI.Contracts.DTOs.File | |||
| { | |||
| public string Title { get; set; } | |||
| public int CategoryId { get; set; } | |||
| public int? CategoryId { get; set; } | |||
| public int[] TagsIds { get; set; } | |||
| public IFormFile FileToUpload { get; set; } | |||
| public string? Note { get; set; } | |||
| } | |||
| } | |||
| @@ -5,7 +5,7 @@ namespace Diligent.WebAPI.Contracts.DTOs.Files | |||
| public class FileFilter : Pagination | |||
| { | |||
| public string[]? Extensions { get; set; } | |||
| public string[]? Categories { get; set; } | |||
| public string[]? Categories { get; set; } = null; | |||
| public string[]? Tags { get; set; } | |||
| public string? Content { get; set; } | |||
| } | |||
| @@ -16,9 +16,9 @@ namespace Diligent.WebAPI.Data.Entities | |||
| public Guid DocumentId { get; set; } | |||
| [ForeignKey(nameof(Category))] | |||
| public int CategoryId { get; set; } | |||
| public int? CategoryId { get; set; } | |||
| public Category Category { get; set; } | |||
| public Category? Category { get; set; } = null; | |||
| public List<Tag> Tags { get; set; } | |||
| @@ -0,0 +1,56 @@ | |||
| using Microsoft.EntityFrameworkCore.Migrations; | |||
| #nullable disable | |||
| namespace Diligent.WebAPI.Data.Migrations | |||
| { | |||
| public partial class SetFileEntityCategoryToBeNullable : Migration | |||
| { | |||
| protected override void Up(MigrationBuilder migrationBuilder) | |||
| { | |||
| migrationBuilder.DropForeignKey( | |||
| name: "FK_Files_Categories_CategoryId", | |||
| table: "Files"); | |||
| migrationBuilder.AlterColumn<int>( | |||
| name: "CategoryId", | |||
| table: "Files", | |||
| type: "int", | |||
| nullable: true, | |||
| oldClrType: typeof(int), | |||
| oldType: "int"); | |||
| migrationBuilder.AddForeignKey( | |||
| name: "FK_Files_Categories_CategoryId", | |||
| table: "Files", | |||
| column: "CategoryId", | |||
| principalTable: "Categories", | |||
| principalColumn: "Id"); | |||
| } | |||
| protected override void Down(MigrationBuilder migrationBuilder) | |||
| { | |||
| migrationBuilder.DropForeignKey( | |||
| name: "FK_Files_Categories_CategoryId", | |||
| table: "Files"); | |||
| migrationBuilder.AlterColumn<int>( | |||
| name: "CategoryId", | |||
| table: "Files", | |||
| type: "int", | |||
| nullable: false, | |||
| defaultValue: 0, | |||
| oldClrType: typeof(int), | |||
| oldType: "int", | |||
| oldNullable: true); | |||
| migrationBuilder.AddForeignKey( | |||
| name: "FK_Files_Categories_CategoryId", | |||
| table: "Files", | |||
| column: "CategoryId", | |||
| principalTable: "Categories", | |||
| principalColumn: "Id", | |||
| onDelete: ReferentialAction.Cascade); | |||
| } | |||
| } | |||
| } | |||
| @@ -262,7 +262,7 @@ namespace Diligent.WebAPI.Data.Migrations | |||
| SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1); | |||
| b.Property<int>("CategoryId") | |||
| b.Property<int?>("CategoryId") | |||
| .HasColumnType("int"); | |||
| b.Property<bool>("Deleted") | |||
| @@ -1015,9 +1015,7 @@ namespace Diligent.WebAPI.Data.Migrations | |||
| { | |||
| b.HasOne("Diligent.WebAPI.Data.Entities.Category", "Category") | |||
| .WithMany("Files") | |||
| .HasForeignKey("CategoryId") | |||
| .OnDelete(DeleteBehavior.Cascade) | |||
| .IsRequired(); | |||
| .HasForeignKey("CategoryId"); | |||
| b.Navigation("Category"); | |||
| }); | |||
| @@ -1113,7 +1111,7 @@ namespace Diligent.WebAPI.Data.Migrations | |||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.UserCategories", b => | |||
| { | |||
| b.HasOne("Diligent.WebAPI.Data.Entities.Category", null) | |||
| b.HasOne("Diligent.WebAPI.Data.Entities.Category", "Category") | |||
| .WithMany("UserCategories") | |||
| .HasForeignKey("CategoryId") | |||
| .OnDelete(DeleteBehavior.Cascade) | |||
| @@ -1124,6 +1122,8 @@ namespace Diligent.WebAPI.Data.Migrations | |||
| .HasForeignKey("UserId") | |||
| .OnDelete(DeleteBehavior.Cascade) | |||
| .IsRequired(); | |||
| b.Navigation("Category"); | |||
| }); | |||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.WebhookSubscription", b => | |||
| @@ -33,9 +33,15 @@ namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| 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); | |||
| Category category = null; | |||
| if(request.CategoryId > 0) | |||
| { | |||
| category = await _categoryService.GetCategoryEntityById(request.CategoryId); | |||
| if (category == null) throw new BadHttpRequestException("Category cannot found"); | |||
| } | |||
| if (category == null) throw new BadHttpRequestException("Category cannot found"); | |||
| List<Tag> tags = new(); | |||
| foreach (var id in request.TagsIds) | |||
| @@ -43,7 +49,7 @@ namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| var file = await _documentService.UploadDocument(request.FileToUpload); | |||
| await _fileEntityService.UploadFileAsync(new FileEntity { CategoryId = request.CategoryId, DocumentId = file.stream_id, Category = category, Tags = tags, Title = request.Title,Note = request.Note}); | |||
| await _fileEntityService.UploadFileAsync(new FileEntity { CategoryId = request.CategoryId > 0 ? request.CategoryId : null, DocumentId = file.stream_id, Category = category, Tags = tags, Title = request.Title}); | |||
| return Ok(); | |||
| } | |||