Changeset 9694de9


Ignore:
Timestamp:
10/21/25 17:21:47 (11 months ago)
Author:
imbrsk <boris696boris@…>
Branches:
master
Children:
aed1148
Parents:
618be76
Message:

Refactor user management: update models, repositories, and services; remove obsolete files

Location:
iknow-api
Files:
2 added
1 deleted
6 edited
3 moved

Legend:

Unmodified
Added
Removed
  • iknow-api/Controllers/UsersControllers.cs

    r618be76 r9694de9  
    1 using iknow_api.Data;
    2 using iknow_api.Models;
     1using iknow_api.Core.Models;
    32using System.Threading.Tasks;
    43using Microsoft.AspNetCore.Mvc;
    5 
     4using iknow_api.Core.Data;
     5using iknow_api.Repository.Interfaces;
     6using iknow_api.Core.Interfaces;
    67
    78namespace iknow_api.Controllers
     
    1112    public class UsersController : ControllerBase
    1213    {
    13         private readonly AppDbContext _context;
    14         private readonly IConfiguration _configuration;
     14        private readonly IUserService _users;
    1515
    16         public UsersController(AppDbContext context, IConfiguration configuration)
     16        public UsersController(IUserService users)
    1717        {
    18             _context = context;
    19             _configuration = configuration;
     18            _users = users;
    2019        }
    2120
    2221        [HttpPost("add")]
    23         public async Task<IActionResult> Add([FromBody] JsonContent body)
     22        public async Task<IActionResult> Add([FromBody] User user)
    2423        {
    25            
    26             return null;
     24            var created = await _users.AddUserAsync(user);
     25            return Ok(created);
    2726        }
    2827
     28        [HttpGet("id/{id}")]
     29        public async Task<IActionResult> GetById(int id)
     30        {
     31            var user = await _users.GetUserByIdAsync(id);
     32            if (user == null)
     33                return NotFound();
     34            return Ok(user);
     35        }
    2936    }
    3037}
  • iknow-api/Data/AppDbContext.cs

    r618be76 r9694de9  
    11using Microsoft.EntityFrameworkCore;
    2 using iknow_api.Models;
     2using iknow_api.Core.Models;
    33
    4 public class AppDbContext : DbContext
     4namespace iknow_api.Core.Data
    55{
    6     public AppDbContext(DbContextOptions<AppDbContext> options)
    7         : base(options) { }
     6    public class AppDbContext : DbContext
     7    {
     8        public AppDbContext(DbContextOptions<AppDbContext> options)
     9            : base(options) { }
    810
    9     public DbSet<Users> Users { get; set; }
     11        public DbSet<User> User { get; set; }
     12    }
    1013}
  • iknow-api/Migrations/20251021141534_InitialCreate.Designer.cs

    r618be76 r9694de9  
    66using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
    77using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
     8using iknow_api.Core.Data;
    89
    910#nullable disable
     
    1213{
    1314    [DbContext(typeof(AppDbContext))]
    14     [Migration("20251020130122_InitialCreate")]
     15    [Migration("20251021141534_InitialCreate")]
    1516    partial class InitialCreate
    1617    {
     
    2526            NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
    2627
    27             modelBuilder.Entity("iknow_api.Models.Users", b =>
     28            modelBuilder.Entity("iknow_api.Core.Models.User", b =>
    2829                {
    2930                    b.Property<int>("Id")
     
    5960                    b.HasKey("Id");
    6061
    61                     b.ToTable("Users");
     62                    b.ToTable("User");
    6263                });
    6364#pragma warning restore 612, 618
  • iknow-api/Migrations/20251021141534_InitialCreate.cs

    r618be76 r9694de9  
    1414        {
    1515            migrationBuilder.CreateTable(
    16                 name: "Users",
     16                name: "User",
    1717                columns: table => new
    1818                {
     
    3030                constraints: table =>
    3131                {
    32                     table.PrimaryKey("PK_Users", x => x.Id);
     32                    table.PrimaryKey("PK_User", x => x.Id);
    3333                });
    3434        }
     
    3838        {
    3939            migrationBuilder.DropTable(
    40                 name: "Users");
     40                name: "User");
    4141        }
    4242    }
  • iknow-api/Migrations/AppDbContextModelSnapshot.cs

    r618be76 r9694de9  
    55using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
    66using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
     7using iknow_api.Core.Data;
    78
    89#nullable disable
     
    2223            NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
    2324
    24             modelBuilder.Entity("iknow_api.Models.Users", b =>
     25            modelBuilder.Entity("iknow_api.Core.Models.User", b =>
    2526                {
    2627                    b.Property<int>("Id")
     
    5657                    b.HasKey("Id");
    5758
    58                     b.ToTable("Users");
     59                    b.ToTable("User");
    5960                });
    6061#pragma warning restore 612, 618
  • iknow-api/Models/User.cs

    r618be76 r9694de9  
    11
    22
    3 namespace iknow_api.Models
     3namespace iknow_api.Core.Models
    44{
    55    public enum UserRole
     
    1010    }
    1111
    12     public class Users
     12    public class User
    1313    {
    1414        public int Id { get; set; }
  • iknow-api/Program.cs

    r618be76 r9694de9  
    11using Microsoft.EntityFrameworkCore;
     2using iknow_api.Core.Data;
    23
    34var builder = WebApplication.CreateBuilder(args);
     
    910builder.Services.AddOpenApi();
    1011
     12
     13builder.Services.AddDbContext<AppDbContext>(options =>
     14    options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
     15
     16builder.Services.AddScoped<iknow_api.Repository.Interfaces.IUserRepository, iknow_api.Repository.UserRepository>();
     17
    1118var app = builder.Build();
    1219
     
    1724}
    1825
    19 builder.Services.AddDbContext<AppDbContext>(options =>
    20     options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
    2126
    2227   
  • iknow-api/Services/Implementations/UserService.cs

    r618be76 r9694de9  
    1 using Microsoft.AspNetCore.Mvc;
     1using Microsoft.EntityFrameworkCore;
     2using iknow_api.Core.Models;
     3using iknow_api.Repository.Interfaces;
     4// ...existing code...
    25
    36namespace iknow_api.Core.Services
     
    58    public class UserService
    69    {
    7         private readonly AppDbContext _context;
     10        private readonly IUserRepository _users;
    811
    9         public UserService(AppDbContext context)
     12        public UserService(IUserRepository users)
    1013        {
    11             _context = context;
     14            _users = users;
    1215        }
    1316
     17        public async Task<User> AddUserAsync(User user, CancellationToken cancellationToken = default)
     18        {
     19            if (user == null) throw new ArgumentNullException(nameof(user));
    1420
     21            if (!string.IsNullOrWhiteSpace(user.Email))
     22            {
     23                user.Email = user.Email.Trim();
     24
     25                var exists = await _users.EmailExistsAsync(user.Email, cancellationToken);
     26                if (exists)
     27                    throw new InvalidOperationException("A user with this email already exists.");
     28            }
     29
     30            return await _users.AddUserAsync(user, cancellationToken);
     31        }
    1532    }
    1633}
  • iknow-api/Services/Interfaces/IUserService.cs

    r618be76 r9694de9  
     1using System.Threading;
     2using System.Threading.Tasks;
     3using iknow_api.Core.Models;
    14
    25namespace iknow_api.Core.Interfaces
     
    47    public interface IUserService
    58    {
    6         Task<JsonContent> AddUser(JsonContent newUser);
     9        Task<User> AddUserAsync(User user, CancellationToken cancellationToken = default);
     10        Task<int> GetUserByIdAsync(int id, CancellationToken cancellationToken = default);
    711    }
    812}
Note: See TracChangeset for help on using the changeset viewer.