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