Преглед изворни кода

Added delete file functionality & changed access to categories

BE_dev
bronjaermin пре 2 година
родитељ
комит
330400379f

+ 26
- 4
Diligent.WebAPI.Business/Services/CategoryService.cs Прегледај датотеку

using Diligent.WebAPI.Contracts.DTOs.Categories;
using Dapper;
using Diligent.WebAPI.Contracts.DTOs.Categories;
using Microsoft.Extensions.Configuration;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
{ {
private readonly DatabaseContext _context; private readonly DatabaseContext _context;
private readonly IMapper _mapper; private readonly IMapper _mapper;
private readonly UserManager<User> _userManager;
private readonly IConfiguration _configuration;


public CategoryService(DatabaseContext context, IMapper mapper)
public CategoryService(DatabaseContext context, IMapper mapper, UserManager<User> userManager, IConfiguration configuration)
{ {
_context = context; _context = context;
_mapper = mapper; _mapper = mapper;
_userManager = userManager;
_configuration = configuration;
} }


public async Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync() =>
_mapper.Map<List<CategoriesNamesResponse>>(await _context.Categories.ToListAsync());
public async Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync(int userId)
{
var user = await _userManager.FindByIdAsync(userId.ToString());
var role = (await _userManager.GetRolesAsync(user))[0];

if(role == "SuperAdmin") return _mapper.Map<List<CategoriesNamesResponse>>(await _context.Categories.ToListAsync());
else
{
using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi"));
var sql = @"SELECT C.Id, C.Name FROM AspNetUsers AS U INNER JOIN UserCategories AS UC ON U.Id = UC.UserId
INNER JOIN Categories AS C ON UC.CategoryId = C.Id WHERE U.Id = @UserId";
var categories = await connection.QueryAsync<CategoriesNamesResponse>(sql, new { UserId = 9 });
var categoriesList = categories.ToList();
return categoriesList;
}

}


public async Task<Category> GetCategoryEntityById(int id) => public async Task<Category> GetCategoryEntityById(int id) =>
await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync(); await _context.Categories.Where(x => x.Id == id).FirstOrDefaultAsync();

+ 26
- 5
Diligent.WebAPI.Business/Services/FileEntityService.cs Прегледај датотеку

} }


public async Task<List<FileEntityResponse>> GetAllAsync() => public async Task<List<FileEntityResponse>> GetAllAsync() =>
_mapper.Map<List<FileEntityResponse>>(await _context.Files.Include(x => x.Tags).ToListAsync());
_mapper.Map<List<FileEntityResponse>>(await _context.Files.Include(x => x.Tags).Where(x => x.Deleted == false).ToListAsync());


public async Task UploadPdfAsync(FileEntity file) public async Task UploadPdfAsync(FileEntity file)
{ {


public async Task<IEnumerable<FileEntity>> GetAll() public async Task<IEnumerable<FileEntity>> GetAll()
{ {
return await _context.Files.ToListAsync();
return await _context.Files.Where(x => x.Deleted == false).ToListAsync();
} }


public async Task<IEnumerable<FileEntity>> GetAllFilesBasedOnContent(String content) public async Task<IEnumerable<FileEntity>> GetAllFilesBasedOnContent(String content)
using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi"));


var sql = @"select * from Files inner join DocumentOrganizerDocStore on Files.DocumentId = DocumentOrganizerDocStore.stream_id var sql = @"select * from Files inner join DocumentOrganizerDocStore on Files.DocumentId = DocumentOrganizerDocStore.stream_id
where contains(DocumentOrganizerDocStore.file_stream,@content)";
where contains(DocumentOrganizerDocStore.file_stream,@content) AND Files.Deleted = 0";


var files = await connection.QueryAsync<FileEntity>(sql,new {content=content}); var files = await connection.QueryAsync<FileEntity>(sql,new {content=content});
return files.ToList(); return files.ToList();
{ {
using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi")); using var connection = new SqlConnection(_configuration.GetConnectionString("WebApi"));
var sql = @"SELECT Files.Id as FileId, stream_id, DocumentOrganizerDocStore.name as FileName, file_stream, file_type, cached_file_size, Title, Tags.Id as TagId, Tags.Name as TagName, Categories.Id as CategoryId, Categories.Name as CategoryName
var sql = @"SELECT Files.Id as FileId, Files.Deleted, stream_id, DocumentOrganizerDocStore.name as FileName, file_stream, file_type, cached_file_size, Title, Tags.Id as TagId, Tags.Name as TagName, Categories.Id as CategoryId, Categories.Name as CategoryName
FROM Files inner join FileEntityTag on Files.Id = FileEntityTag.FilesId FROM Files inner join FileEntityTag on Files.Id = FileEntityTag.FilesId
inner join Tags on FileEntityTag.TagsId = Tags.Id inner join Tags on FileEntityTag.TagsId = Tags.Id
inner join Categories on Files.CategoryId = Categories.Id inner join Categories on Files.CategoryId = Categories.Id
inner join DocumentOrganizerDocStore on DocumentOrganizerDocStore.stream_id = Files.DocumentId" +
inner join DocumentOrganizerDocStore on DocumentOrganizerDocStore.stream_id = Files.DocumentId WHERE Files.Deleted = 0" +
(filters.Content is null ? "" : $" where contains(DocumentOrganizerDocStore.file_stream,@content);"); (filters.Content is null ? "" : $" where contains(DocumentOrganizerDocStore.file_stream,@content);");


var files = await connection.QueryAsync<FileFilterReturnDto, TagResponse, CategoryResponse, FileFilterReturnDto>(sql,(file, tag, category) => var files = await connection.QueryAsync<FileFilterReturnDto, TagResponse, CategoryResponse, FileFilterReturnDto>(sql,(file, tag, category) =>
Total = filtered.Count Total = filtered.Count
}; };
} }

public async Task<FileEntity> GetFileEntityByIdAsync(Guid id)
{
var file = await _context.Files.Where(x => x.DocumentId == id).FirstOrDefaultAsync();

if (file is null)
{
throw new EntityNotFoundException("File not found");
}

return file;
}

public async Task DeleteFileAsync(Guid id)
{
var file = await GetFileEntityByIdAsync(id);

file.Deleted= true;

await _context.SaveChangesAsync();
}
} }
} }

+ 1
- 1
Diligent.WebAPI.Business/Services/Interfaces/ICategoryService.cs Прегледај датотеку

{ {
public interface ICategoryService public interface ICategoryService
{ {
Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync();
Task<List<CategoriesNamesResponse>> GetCategoriesNamesAsync(int userId);
Task<Category> GetCategoryEntityById(int id); Task<Category> GetCategoryEntityById(int id);
Task<List<IsGrantedCategory>> GetCategories(int userId); Task<List<IsGrantedCategory>> GetCategories(int userId);
} }

+ 2
- 0
Diligent.WebAPI.Business/Services/Interfaces/IFileEntityService.cs Прегледај датотеку

Task<IEnumerable<FileEntity>> GetAll(); Task<IEnumerable<FileEntity>> GetAll();
Task<IEnumerable<FileEntity>> GetAllFilesBasedOnContent(string content); Task<IEnumerable<FileEntity>> GetAllFilesBasedOnContent(string content);
Task<object> GetAllFiltered(FileFilter filters); Task<object> GetAllFiltered(FileFilter filters);
Task<FileEntity> GetFileEntityByIdAsync(Guid id);
Task DeleteFileAsync(Guid id);
} }
} }

+ 1
- 3
Diligent.WebAPI.Business/Services/UserService.cs Прегледај датотеку

private readonly DatabaseContext _databaseContext; private readonly DatabaseContext _databaseContext;
private readonly IEmailer _emailer; private readonly IEmailer _emailer;
private readonly ILogger<UserService> _logger; private readonly ILogger<UserService> _logger;
private readonly ICategoryService _categoryService;


public UserService(IOptions<FrontEndSettings> frontEndSettings, UserManager<User> userManager, IMapper mapper, DatabaseContext databaseContext, IEmailer emailer, ILogger<UserService> logger, ICategoryService categoryService)
public UserService(IOptions<FrontEndSettings> frontEndSettings, UserManager<User> userManager, IMapper mapper, DatabaseContext databaseContext, IEmailer emailer, ILogger<UserService> logger)
{ {
_frontEndSettings = frontEndSettings.Value; _frontEndSettings = frontEndSettings.Value;
_userManager = userManager; _userManager = userManager;
_databaseContext = databaseContext; _databaseContext = databaseContext;
_emailer = emailer; _emailer = emailer;
_logger = logger; _logger = logger;
_categoryService = categoryService;
} }


public async Task<IEnumerable<User?>> GetAll() public async Task<IEnumerable<User?>> GetAll()

+ 2
- 0
Diligent.WebAPI.Contracts/DTOs/File/FileEntityResponse.cs Прегледај датотеку

public string Extension { get; set; } public string Extension { get; set; }


public List<TagResponse> Tags { get; set; } public List<TagResponse> Tags { get; set; }

public bool Deleted { get; set; }
} }
} }

+ 1
- 0
Diligent.WebAPI.Contracts/DTOs/Files/FileFilterReturnDto.cs Прегледај датотеку

public int cached_file_size { get; set; } public int cached_file_size { get; set; }
public List<TagResponse> Tags { get; set; } = new(); public List<TagResponse> Tags { get; set; } = new();
public CategoryResponse Category { get; set; } public CategoryResponse Category { get; set; }
public bool Deleted { get; set; }
} }
} }

+ 2
- 0
Diligent.WebAPI.Data/Entities/FileEntity.cs Прегледај датотеку

public Category Category { get; set; } public Category Category { get; set; }


public List<Tag> Tags { get; set; } public List<Tag> Tags { get; set; }

public bool Deleted { get; set; } = false;
} }
} }

+ 1227
- 0
Diligent.WebAPI.Data/Migrations/20230221101523_AddedDeletedFieldToFileEntity.Designer.cs
Разлика између датотеке није приказан због своје велике величине
Прегледај датотеку


+ 26
- 0
Diligent.WebAPI.Data/Migrations/20230221101523_AddedDeletedFieldToFileEntity.cs Прегледај датотеку

using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace Diligent.WebAPI.Data.Migrations
{
public partial class AddedDeletedFieldToFileEntity : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "Deleted",
table: "Files",
type: "bit",
nullable: false,
defaultValue: false);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Deleted",
table: "Files");
}
}
}

+ 3
- 0
Diligent.WebAPI.Data/Migrations/DatabaseContextModelSnapshot.cs Прегледај датотеку

b.Property<int>("CategoryId") b.Property<int>("CategoryId")
.HasColumnType("int"); .HasColumnType("int");


b.Property<bool>("Deleted")
.HasColumnType("bit");

b.Property<Guid>("DocumentId") b.Property<Guid>("DocumentId")
.HasColumnType("uniqueidentifier"); .HasColumnType("uniqueidentifier");



+ 5
- 2
Diligent.WebAPI.Host/Controllers/V1/CategoriesController.cs Прегледај датотеку

} }


[HttpGet("names")] [HttpGet("names")]
public async Task<IActionResult> GetCategoriesNames() =>
Ok(await _categoryService.GetCategoriesNamesAsync());
public async Task<IActionResult> GetCategoriesNames()
{
User? user = (User?)HttpContext.Items["User"];
return Ok(await _categoryService.GetCategoriesNamesAsync(user.Id));
}


[HttpGet("granted-categories")] [HttpGet("granted-categories")]
public async Task<IActionResult> GetCategories(int userId) => public async Task<IActionResult> GetCategories(int userId) =>

+ 7
- 0
Diligent.WebAPI.Host/Controllers/V1/FilesController.cs Прегледај датотеку

await _fileEntityService.UploadPdfAsync(new FileEntity { CategoryId = request.CategoryId, DocumentId = file.stream_id, Category = category, Tags = tags, Title = request.Title}); await _fileEntityService.UploadPdfAsync(new FileEntity { CategoryId = request.CategoryId, DocumentId = file.stream_id, Category = category, Tags = tags, Title = request.Title});
return Ok(); return Ok();
} }

[HttpDelete("delete-file/{id}")]
public async Task<IActionResult> DeleteFile([FromRoute]Guid id)
{
await _fileEntityService.DeleteFileAsync(id);
return Ok();
}
} }
} }

Loading…
Откажи
Сачувај