Changes in / [c0a75ab:aed1148]
- Location:
- iknow-api
- Files:
-
- 5 added
- 4 deleted
- 6 edited
-
Controllers/UsersControllers.cs (modified) (2 diffs)
-
Data/AppDbContext.cs (modified) (1 diff)
-
Data/AppDbContextFactory.cs (deleted)
-
Migrations/20251020130122_InitialCreate.Designer.cs (deleted)
-
Migrations/20251020130122_InitialCreate.cs (deleted)
-
Migrations/20251021141534_InitialCreate.Designer.cs (added)
-
Migrations/20251021141534_InitialCreate.cs (added)
-
Migrations/AppDbContextModelSnapshot.cs (modified) (3 diffs)
-
Models/User.cs (added)
-
Models/Users.cs (deleted)
-
Program.cs (modified) (3 diffs)
-
Repository/Implementations/UserRepository.cs (added)
-
Repository/Interface/IUserRepository.cs (added)
-
Services/Implementations/UserService.cs (modified) (2 diffs)
-
Services/Interfaces/IUserService.cs (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
iknow-api/Controllers/UsersControllers.cs
rc0a75ab raed1148 1 //using iknow_api.Data; 2 using iknow_api.Models; 1 using iknow_api.Core.Models; 3 2 using System.Threading.Tasks; 4 3 using Microsoft.AspNetCore.Mvc; 5 4 using iknow_api.Core.Data; 5 using iknow_api.Repository.Interfaces; 6 using iknow_api.Core.Interfaces; 6 7 7 8 namespace iknow_api.Controllers … … 11 12 public class UsersController : ControllerBase 12 13 { 13 private readonly AppDbContext _context; 14 private readonly IConfiguration _configuration; 14 private readonly IUserService _users; 15 15 16 public UsersController( AppDbContext context, IConfiguration configuration)16 public UsersController(IUserService users) 17 17 { 18 _context = context; 19 _configuration = configuration; 18 _users = users; 20 19 } 21 20 22 21 [HttpPost("add")] 23 public async Task<IActionResult> Add([FromBody] JsonContent body)22 public async Task<IActionResult> Add([FromBody] User user) 24 23 { 25 26 return null;24 var created = await _users.AddUserAsync(user); 25 return Ok(created); 27 26 } 28 27 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 } 29 36 } 30 37 } -
iknow-api/Data/AppDbContext.cs
rc0a75ab raed1148 1 1 using Microsoft.EntityFrameworkCore; 2 using iknow_api. Models;2 using iknow_api.Core.Models; 3 3 4 public class AppDbContext : DbContext 4 namespace iknow_api.Core.Data 5 5 { 6 public AppDbContext(DbContextOptions<AppDbContext> options) 7 : base(options) { } 6 public class AppDbContext : DbContext 7 { 8 public AppDbContext(DbContextOptions<AppDbContext> options) 9 : base(options) { } 8 10 9 public DbSet<Users> Users { get; set; } 11 public DbSet<User> User { get; set; } 12 } 10 13 } -
iknow-api/Migrations/AppDbContextModelSnapshot.cs
rc0a75ab raed1148 5 5 using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 6 6 using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; 7 using iknow_api.Core.Data; 7 8 8 9 #nullable disable … … 22 23 NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); 23 24 24 modelBuilder.Entity("iknow_api. Models.Users", b =>25 modelBuilder.Entity("iknow_api.Core.Models.User", b => 25 26 { 26 27 b.Property<int>("Id") … … 56 57 b.HasKey("Id"); 57 58 58 b.ToTable("User s");59 b.ToTable("User"); 59 60 }); 60 61 #pragma warning restore 612, 618 -
iknow-api/Program.cs
rc0a75ab raed1148 1 1 using Microsoft.EntityFrameworkCore; 2 using iknow_api.Core.Data; 2 3 3 4 var builder = WebApplication.CreateBuilder(args); … … 9 10 builder.Services.AddOpenApi(); 10 11 12 11 13 builder.Services.AddDbContext<AppDbContext>(options => 12 14 options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))); 15 16 builder.Services.AddScoped<iknow_api.Repository.Interfaces.IUserRepository, iknow_api.Repository.UserRepository>(); 13 17 14 18 var app = builder.Build(); … … 20 24 } 21 25 22 26 builder.Services.AddDbContext<AppDbContext>(options => 27 options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))); 23 28 24 29 -
iknow-api/Services/Implementations/UserService.cs
rc0a75ab raed1148 1 using Microsoft.AspNetCore.Mvc; 1 using Microsoft.EntityFrameworkCore; 2 using iknow_api.Core.Models; 3 using iknow_api.Repository.Interfaces; 4 // ...existing code... 2 5 3 6 namespace iknow_api.Core.Services … … 5 8 public class UserService 6 9 { 7 private readonly AppDbContext _context;10 private readonly IUserRepository _users; 8 11 9 public UserService( AppDbContext context)12 public UserService(IUserRepository users) 10 13 { 11 _ context = context;14 _users = users; 12 15 } 13 16 17 public async Task<User> AddUserAsync(User user, CancellationToken cancellationToken = default) 18 { 19 if (user == null) throw new ArgumentNullException(nameof(user)); 14 20 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 } 15 32 } 16 33 } -
iknow-api/Services/Interfaces/IUserService.cs
rc0a75ab raed1148 1 using System.Threading; 2 using System.Threading.Tasks; 3 using iknow_api.Core.Models; 1 4 2 5 namespace iknow_api.Core.Interfaces … … 4 7 public interface IUserService 5 8 { 6 Task<JsonContent> AddUser(JsonContent newUser); 9 Task<User> AddUserAsync(User user, CancellationToken cancellationToken = default); 10 Task<int> GetUserByIdAsync(int id, CancellationToken cancellationToken = default); 7 11 } 8 12 }
Note:
See TracChangeset
for help on using the changeset viewer.
