Changes in / [aed1148:c0a75ab]


Ignore:
Location:
iknow-api
Files:
4 added
5 deleted
6 edited

Legend:

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

    raed1148 rc0a75ab  
    1 using iknow_api.Core.Models;
     1//using iknow_api.Data;
     2using iknow_api.Models;
    23using System.Threading.Tasks;
    34using Microsoft.AspNetCore.Mvc;
    4 using iknow_api.Core.Data;
    5 using iknow_api.Repository.Interfaces;
    6 using iknow_api.Core.Interfaces;
     5
    76
    87namespace iknow_api.Controllers
    … …  
    1211    public class UsersController : ControllerBase
    1312    {
    14         private readonly IUserService _users;
     13        private readonly AppDbContext _context;
     14        private readonly IConfiguration _configuration;
    1515
    16         public UsersController(IUserService users)
     16        public UsersController(AppDbContext context, IConfiguration configuration)
    1717        {
    18             _users = users;
     18            _context = context;
     19            _configuration = configuration;
    1920        }
    2021
    2122        [HttpPost("add")]
    22         public async Task<IActionResult> Add([FromBody] User user)
     23        public async Task<IActionResult> Add([FromBody] JsonContent body)
    2324        {
    24             var created = await _users.AddUserAsync(user);
    25             return Ok(created);
     25           
     26            return null;
    2627        }
    2728
    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         }
    3629    }
    3730}
  • iknow-api/Data/AppDbContext.cs

    raed1148 rc0a75ab  
    11using Microsoft.EntityFrameworkCore;
    2 using iknow_api.Core.Models;
     2using iknow_api.Models;
    33
    4 namespace iknow_api.Core.Data
     4public class AppDbContext : DbContext
    55{
    6     public class AppDbContext : DbContext
    7     {
    8         public AppDbContext(DbContextOptions<AppDbContext> options)
    9             : base(options) { }
     6    public AppDbContext(DbContextOptions<AppDbContext> options)
     7        : base(options) { }
    108
    11         public DbSet<User> User { get; set; }
    12     }
     9    public DbSet<Users> Users { get; set; }
    1310}
  • iknow-api/Migrations/AppDbContextModelSnapshot.cs

    raed1148 rc0a75ab  
    55using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
    66using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
    7 using iknow_api.Core.Data;
    87
    98#nullable disable
    … …  
    2322            NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
    2423
    25             modelBuilder.Entity("iknow_api.Core.Models.User", b =>
     24            modelBuilder.Entity("iknow_api.Models.Users", b =>
    2625                {
    2726                    b.Property<int>("Id")
    … …  
    5756                    b.HasKey("Id");
    5857
    59                     b.ToTable("User");
     58                    b.ToTable("Users");
    6059                });
    6160#pragma warning restore 612, 618
  • iknow-api/Program.cs

    raed1148 rc0a75ab  
    11using Microsoft.EntityFrameworkCore;
    2 using iknow_api.Core.Data;
    32
    43var builder = WebApplication.CreateBuilder(args);
    … …  
    109builder.Services.AddOpenApi();
    1110
    12 
    1311builder.Services.AddDbContext<AppDbContext>(options =>
    1412    options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
    15 
    16 builder.Services.AddScoped<iknow_api.Repository.Interfaces.IUserRepository, iknow_api.Repository.UserRepository>();
    1713
    1814var app = builder.Build();
    … …  
    2420}
    2521
    26 builder.Services.AddDbContext<AppDbContext>(options =>
    27     options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
     22
    2823
    2924   
  • iknow-api/Services/Implementations/UserService.cs

    raed1148 rc0a75ab  
    1 using Microsoft.EntityFrameworkCore;
    2 using iknow_api.Core.Models;
    3 using iknow_api.Repository.Interfaces;
    4 // ...existing code...
     1using Microsoft.AspNetCore.Mvc;
    52
    63namespace iknow_api.Core.Services
    … …  
    85    public class UserService
    96    {
    10         private readonly IUserRepository _users;
     7        private readonly AppDbContext _context;
    118
    12         public UserService(IUserRepository users)
     9        public UserService(AppDbContext context)
    1310        {
    14             _users = users;
     11            _context = context;
    1512        }
    1613
    17         public async Task<User> AddUserAsync(User user, CancellationToken cancellationToken = default)
    18         {
    19             if (user == null) throw new ArgumentNullException(nameof(user));
    2014
    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         }
    3215    }
    3316}
  • iknow-api/Services/Interfaces/IUserService.cs

    raed1148 rc0a75ab  
    1 using System.Threading;
    2 using System.Threading.Tasks;
    3 using iknow_api.Core.Models;
    41
    52namespace iknow_api.Core.Interfaces
    … …  
    74    public interface IUserService
    85    {
    9         Task<User> AddUserAsync(User user, CancellationToken cancellationToken = default);
    10         Task<int> GetUserByIdAsync(int id, CancellationToken cancellationToken = default);
     6        Task<JsonContent> AddUser(JsonContent newUser);
    117    }
    128}
Note: See TracChangeset for help on using the changeset viewer.