// Step 1: Create a new solution and project
dotnet new sln -n Authantication
dotnet new webapi -n WebAPI
dotnet new classlib -n Domain
dotnet new classlib -n Application
dotnet new classlib -n Infrastructure
dotnet sln add WebAPI/WebAPI.csproj Domain/Domain.csproj Application/Application.csproj Infrastructure/Infrastructure.csproj
// Step 2: Add references
dotnet add WebAPI reference Application
dotnet add Application reference Domain
dotnet add Infrastructure reference Domain
dotnet add Application reference Infrastructure
// Step 3: Install packages
cd WebAPI
dotnet add package Microsoft.AspNetCore.Identity.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.AspNetCore.Authentication.JwtBearer
// Step 4: Create Model in Domain
File: Domain/Models/ApplicationUser.cs
namespace Domain.Models;
using Microsoft.AspNetCore.Identity;
public class ApplicationUser : IdentityUser
{
public string? FullName { get; set; }
}
// Step 5: Create DbContext in Infrastructure
// File: Infrastructure/Data/ApplicationDbContext.cs
using Domain.Models;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
namespace Infrastructure.Data;
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
public DbSet<Product> Products { get; set; }
}
// Step 6: Add Product model in Domain
namespace Domain.Models;
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
}
// Step 7: Create Repository Interface
// File: Domain/Interfaces/IProductRepository.cs
namespace Domain.Interfaces;
public interface IProductRepository
{
Task<IEnumerable<Product>> GetAllAsync();
Task<Product> GetByIdAsync(int id);
Task AddAsync(Product product);
Task UpdateAsync(Product product);
Task DeleteAsync(int id);
}
// Step 8: Implement Repository
// File: Infrastructure/Repositories/ProductRepository.cs
using Domain.Interfaces;
using Domain.Models;
using Infrastructure.Data;
using Microsoft.EntityFrameworkCore;
namespace Infrastructure.Repositories;
public class ProductRepository : IProductRepository
{
private readonly ApplicationDbContext _context;
public ProductRepository(ApplicationDbContext context) => _context = context;
public async Task<IEnumerable<Product>> GetAllAsync() => await _context.Products.ToListAsync();
public async Task<Product> GetByIdAsync(int id) => await _context.Products.FindAsync(id);
public async Task AddAsync(Product product) { _context.Products.Add(product); await _context.SaveChangesAsync(); }
public async Task UpdateAsync(Product product) { _context.Products.Update(product); await _context.SaveChangesAsync(); }
public async Task DeleteAsync(int id) { var product = await _context.Products.FindAsync(id); _context.Products.Remove(product); await _context.SaveChangesAsync(); }
}
// Step 9: Create Service Interface
// File: Application/Interfaces/IProductService.cs
using Domain.Models;
namespace Application.Interfaces;
public interface IProductService
{
Task<IEnumerable<Product>> GetAllAsync();
Task<Product> GetByIdAsync(int id);
Task AddAsync(Product product);
Task UpdateAsync(Product product);
Task DeleteAsync(int id);
}
// Step 10: Implement Service
// File: Application/Services/ProductService.cs
using Application.Interfaces;
using Domain.Interfaces;
using Domain.Models;
namespace Application.Services;
public class ProductService : IProductService
{
private readonly IProductRepository _repo;
public ProductService(IProductRepository repo) => _repo = repo;
public Task<IEnumerable<Product>> GetAllAsync() => _repo.GetAllAsync();
public Task<Product> GetByIdAsync(int id) => _repo.GetByIdAsync(id);
public Task AddAsync(Product product) => _repo.AddAsync(product);
public Task UpdateAsync(Product product) => _repo.UpdateAsync(product);
public Task DeleteAsync(int id) => _repo.DeleteAsync(id);
}
// Step 11: Create Controller
// File: WebAPI/Controllers/ProductController.cs
using Application.Interfaces;
using Domain.Models;
using Microsoft.AspNetCore.Mvc;
namespace WebAPI.Controllers;
[Route("api/[controller]")]
[ApiController]
public class ProductController : ControllerBase
{
private readonly IProductService _service;
public ProductController(IProductService service) => _service = service;
[HttpGet] public async Task<IActionResult> Get() => Ok(await _service.GetAllAsync());
[HttpGet("{id}")] public async Task<IActionResult> Get(int id) => Ok(await _service.GetByIdAsync(id));
[HttpPost] public async Task<IActionResult> Post([FromBody] Product product) { await _service.AddAsync(product); return Ok(); }
[HttpPut("{id}")] public async Task<IActionResult> Put(int id, [FromBody] Product product) { product.Id = id; await _service.UpdateAsync(product); return Ok(); }
[HttpDelete("{id}")] public async Task<IActionResult> Delete(int id) { await _service.DeleteAsync(id); return Ok(); }
}
// Step 12: Update Program.cs
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddScoped<IProductRepository, ProductRepository>();
builder.Services.AddScoped<IProductService, ProductService>();
builder.Services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(...); // your JWT config here
Comments
Post a Comment