| @@ -12,5 +12,6 @@ namespace Diligent.WebAPI.Business.Services.Interfaces | |||
| Task RemoveUser(User user); | |||
| Task<ServiceResponseDTO<object>> SendRegistrationLink(InviteDTO invite); | |||
| Task<User> GetFirst(); | |||
| Task GrantCategoryToUserAsync(GrantUserCategoryRequestDto request); | |||
| } | |||
| } | |||
| @@ -127,5 +127,15 @@ | |||
| Data = new { Message = "Link has been sent!" } | |||
| }; | |||
| } | |||
| public async Task GrantCategoryToUserAsync(GrantUserCategoryRequestDto request) | |||
| { | |||
| for (int i = 0; i < request.CategoriesId.Count; i++) | |||
| { | |||
| await _databaseContext.UserCategories.AddAsync(new UserCategories { UserId = request.UserId, CategoryId = request.CategoriesId[i] }); | |||
| } | |||
| await _databaseContext.SaveChangesAsync(); | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,15 @@ | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Text; | |||
| using System.Threading.Tasks; | |||
| namespace Diligent.WebAPI.Contracts.DTOs.User | |||
| { | |||
| public class GrantUserCategoryRequestDto | |||
| { | |||
| public int UserId { get; set; } | |||
| public List<int>CategoriesId { get; set; } | |||
| } | |||
| } | |||
| @@ -21,6 +21,7 @@ public class DatabaseContext : IdentityDbContext<User, AppRole, int> | |||
| public DbSet<FileEntity> Files { get; set; } | |||
| public DbSet<Category> Categories { get; set; } | |||
| public DbSet<Tag> Tags { get; set; } | |||
| public DbSet<UserCategories> UserCategories { get; set; } | |||
| public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options) { } | |||
| @@ -14,5 +14,7 @@ namespace Diligent.WebAPI.Data.Entities | |||
| public string Name { get; set; } | |||
| public List<FileEntity> Files { get; set; } | |||
| public List<UserCategories> UserCategories { get; set; } | |||
| } | |||
| } | |||
| @@ -11,4 +11,5 @@ public class User : IdentityUser<int> | |||
| public List<SelectionProcess> Processes { get; set; } = new(); | |||
| public string? Position { get; set; } | |||
| public string? LinkedIn { get; set; } | |||
| public List<UserCategories> UserCategories { get; set; } | |||
| } | |||
| @@ -0,0 +1,17 @@ | |||
| using System; | |||
| using System.Collections.Generic; | |||
| using System.Linq; | |||
| using System.Text; | |||
| using System.Threading.Tasks; | |||
| namespace Diligent.WebAPI.Data.Entities | |||
| { | |||
| public class UserCategories | |||
| { | |||
| public int Id { get; set; } | |||
| public int UserId { get; set; } | |||
| public int CategoryId { get; set; } | |||
| } | |||
| } | |||
| @@ -0,0 +1,54 @@ | |||
| using Microsoft.EntityFrameworkCore.Migrations; | |||
| #nullable disable | |||
| namespace Diligent.WebAPI.Data.Migrations | |||
| { | |||
| public partial class UserCategoryManyToMany : Migration | |||
| { | |||
| protected override void Up(MigrationBuilder migrationBuilder) | |||
| { | |||
| migrationBuilder.CreateTable( | |||
| name: "UserCategories", | |||
| columns: table => new | |||
| { | |||
| Id = table.Column<int>(type: "int", nullable: false) | |||
| .Annotation("SqlServer:Identity", "1, 1"), | |||
| UserId = table.Column<int>(type: "int", nullable: false), | |||
| CategoryId = table.Column<int>(type: "int", nullable: false) | |||
| }, | |||
| constraints: table => | |||
| { | |||
| table.PrimaryKey("PK_UserCategories", x => x.Id); | |||
| table.ForeignKey( | |||
| name: "FK_UserCategories_AspNetUsers_UserId", | |||
| column: x => x.UserId, | |||
| principalTable: "AspNetUsers", | |||
| principalColumn: "Id", | |||
| onDelete: ReferentialAction.Cascade); | |||
| table.ForeignKey( | |||
| name: "FK_UserCategories_Categories_CategoryId", | |||
| column: x => x.CategoryId, | |||
| principalTable: "Categories", | |||
| principalColumn: "Id", | |||
| onDelete: ReferentialAction.Cascade); | |||
| }); | |||
| migrationBuilder.CreateIndex( | |||
| name: "IX_UserCategories_CategoryId", | |||
| table: "UserCategories", | |||
| column: "CategoryId"); | |||
| migrationBuilder.CreateIndex( | |||
| name: "IX_UserCategories_UserId", | |||
| table: "UserCategories", | |||
| column: "UserId"); | |||
| } | |||
| protected override void Down(MigrationBuilder migrationBuilder) | |||
| { | |||
| migrationBuilder.DropTable( | |||
| name: "UserCategories"); | |||
| } | |||
| } | |||
| } | |||
| @@ -730,6 +730,29 @@ namespace Diligent.WebAPI.Data.Migrations | |||
| b.ToTable("AspNetUsers", (string)null); | |||
| }); | |||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.UserCategories", b => | |||
| { | |||
| b.Property<int>("Id") | |||
| .ValueGeneratedOnAdd() | |||
| .HasColumnType("int"); | |||
| SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1); | |||
| b.Property<int>("CategoryId") | |||
| .HasColumnType("int"); | |||
| b.Property<int>("UserId") | |||
| .HasColumnType("int"); | |||
| b.HasKey("Id"); | |||
| b.HasIndex("CategoryId"); | |||
| b.HasIndex("UserId"); | |||
| b.ToTable("UserCategories"); | |||
| }); | |||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.WebhookDefinition", b => | |||
| { | |||
| b.Property<long>("Id") | |||
| @@ -1067,6 +1090,21 @@ namespace Diligent.WebAPI.Data.Migrations | |||
| b.Navigation("Technology"); | |||
| }); | |||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.UserCategories", b => | |||
| { | |||
| b.HasOne("Diligent.WebAPI.Data.Entities.Category", null) | |||
| .WithMany("UserCategories") | |||
| .HasForeignKey("CategoryId") | |||
| .OnDelete(DeleteBehavior.Cascade) | |||
| .IsRequired(); | |||
| b.HasOne("Diligent.WebAPI.Data.Entities.User", null) | |||
| .WithMany("UserCategories") | |||
| .HasForeignKey("UserId") | |||
| .OnDelete(DeleteBehavior.Cascade) | |||
| .IsRequired(); | |||
| }); | |||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.WebhookSubscription", b => | |||
| { | |||
| b.HasOne("Diligent.WebAPI.Data.Entities.WebhookDefinition", "WebhookDefinition") | |||
| @@ -1156,6 +1194,8 @@ namespace Diligent.WebAPI.Data.Migrations | |||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.Category", b => | |||
| { | |||
| b.Navigation("Files"); | |||
| b.Navigation("UserCategories"); | |||
| }); | |||
| modelBuilder.Entity("Diligent.WebAPI.Data.Entities.SelectionLevel", b => | |||
| @@ -1173,6 +1213,8 @@ namespace Diligent.WebAPI.Data.Migrations | |||
| b.Navigation("Comments"); | |||
| b.Navigation("Processes"); | |||
| b.Navigation("UserCategories"); | |||
| }); | |||
| #pragma warning restore 612, 618 | |||
| } | |||
| @@ -66,5 +66,13 @@ namespace Diligent.WebAPI.Host.Controllers.V1 | |||
| return Ok(response.Data); | |||
| } | |||
| [HttpPost("grant-category")] | |||
| public async Task<IActionResult> GrantCategoryToUser([FromBody]GrantUserCategoryRequestDto request) | |||
| { | |||
| await _userService.GrantCategoryToUserAsync(request); | |||
| return StatusCode(201); | |||
| } | |||
| } | |||
| } | |||