Last active
February 17, 2025 09:58
-
-
Save fnmssteam/f2fc1f06fe5ffb9641e62262c12d6d3e to your computer and use it in GitHub Desktop.
DeepSeek EF Core Challenge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.EntityFrameworkCore.Migrations; | |
#nullable disable | |
namespace DeepSeekCore.Migrations | |
{ | |
/// <inheritdoc /> | |
public partial class InitialCreate : Migration | |
{ | |
/// <inheritdoc /> | |
protected override void Up(MigrationBuilder migrationBuilder) | |
{ | |
migrationBuilder.CreateTable( | |
name: "Products", | |
columns: table => new | |
{ | |
Id = table.Column<int>(type: "int", nullable: false) | |
.Annotation("SqlServer:Identity", "1, 1"), | |
Name = table.Column<string>(type: "nvarchar(max)", nullable: false), | |
Price = table.Column<decimal>(type: "decimal(18,2)", nullable: false), | |
StockInQuantity = table.Column<int>(type: "int", nullable: false), | |
IsDiscontinued = table.Column<bool>(type: "bit", nullable: false) | |
}, | |
constraints: table => | |
{ | |
table.PrimaryKey("PK_Products", x => x.Id); | |
}); | |
migrationBuilder.CreateTable( | |
name: "Suppliers", | |
columns: table => new | |
{ | |
Id = table.Column<int>(type: "int", nullable: false) | |
.Annotation("SqlServer:Identity", "1, 1"), | |
Name = table.Column<string>(type: "nvarchar(max)", nullable: false), | |
ContactEmail = table.Column<string>(type: "nvarchar(max)", nullable: false) | |
}, | |
constraints: table => | |
{ | |
table.PrimaryKey("PK_Suppliers", x => x.Id); | |
}); | |
migrationBuilder.CreateTable( | |
name: "ProductSupplier", | |
columns: table => new | |
{ | |
ProductsId = table.Column<int>(type: "int", nullable: false), | |
SuppliersId = table.Column<int>(type: "int", nullable: false) | |
}, | |
constraints: table => | |
{ | |
table.PrimaryKey("PK_ProductSupplier", x => new { x.ProductsId, x.SuppliersId }); | |
table.ForeignKey( | |
name: "FK_ProductSupplier_Products_ProductsId", | |
column: x => x.ProductsId, | |
principalTable: "Products", | |
principalColumn: "Id", | |
onDelete: ReferentialAction.Cascade); | |
table.ForeignKey( | |
name: "FK_ProductSupplier_Suppliers_SuppliersId", | |
column: x => x.SuppliersId, | |
principalTable: "Suppliers", | |
principalColumn: "Id", | |
onDelete: ReferentialAction.Cascade); | |
}); | |
migrationBuilder.CreateIndex( | |
name: "IX_ProductSupplier_SuppliersId", | |
table: "ProductSupplier", | |
column: "SuppliersId"); | |
} | |
/// <inheritdoc /> | |
protected override void Down(MigrationBuilder migrationBuilder) | |
{ | |
migrationBuilder.DropTable( | |
name: "ProductSupplier"); | |
migrationBuilder.DropTable( | |
name: "Products"); | |
migrationBuilder.DropTable( | |
name: "Suppliers"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using DeepSeekCore.Entities; | |
using Microsoft.EntityFrameworkCore; | |
namespace DeepSeekCore; | |
public class DeepSeekCoreContext : DbContext | |
{ | |
public DbSet<Product> Products { get; set; } | |
public DbSet<Supplier> Suppliers { get; set; } | |
protected override void OnConfiguring(DbContextOptionsBuilder options) | |
{ | |
options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=DeepSeekCore;Trusted_Connection=True;"); | |
} | |
protected override void OnModelCreating(ModelBuilder builder) | |
{ | |
builder.Entity<Product>() | |
.HasMany(p => p.Suppliers) | |
.WithMany(s => s.Products); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace DeepSeekCore.Entities; | |
public class Product | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public decimal Price { get; set; } | |
public int StockInQuantity { get; set; } | |
public bool IsDiscontinued { get; set; } | |
public List<Supplier> Suppliers { get; set; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using DeepSeekCore.Entities; | |
namespace DeepSeekCore.Repositories; | |
public class ProductRepository | |
{ | |
private readonly DeepSeekCoreContext _context; | |
public ProductRepository(DeepSeekCoreContext context) | |
{ | |
_context = context; | |
} | |
public void AddProduct(Product product, List<int> supplierIds) | |
{ | |
var suppliers = _context.Suppliers | |
.Where(s => supplierIds.Contains(s.Id)) | |
.ToList(); | |
if (suppliers.Count == 0) | |
{ | |
throw new Exception("None of the suppliers exist."); | |
} | |
product.Suppliers.AddRange(suppliers); | |
_context.SaveChanges(); | |
} | |
public List<Product> GetProductsBySupplier(int supplierId) | |
{ | |
var supplier = _context.Suppliers.Find(supplierId); | |
if (supplier == null) | |
{ | |
throw new Exception("Supplier not found."); | |
} | |
return supplier.Products; | |
} | |
public void UpdateStock(int productId, int newStock) | |
{ | |
var product = _context.Products.Find(productId); | |
if (product == null) | |
{ | |
throw new Exception("Product not found."); | |
} | |
product.StockInQuantity = newStock; | |
_context.SaveChanges(); | |
} | |
public void DiscontinueProducts(int threshold) | |
{ | |
var productsToBeDiscontinued = _context.Products | |
.Where(p => p.StockInQuantity < threshold) | |
.ToList(); | |
if (productsToBeDiscontinued.Count == 0) | |
{ | |
throw new Exception("No product(s) to discontinue."); | |
} | |
foreach (var product in productsToBeDiscontinued) | |
{ | |
product.IsDiscontinued = true; | |
} | |
_context.SaveChanges(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using DeepSeekCore; | |
var builder = WebApplication.CreateBuilder(args); | |
// Add services to the container. | |
builder.Services.AddControllers(); | |
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle | |
builder.Services.AddEndpointsApiExplorer(); | |
builder.Services.AddSwaggerGen(); | |
builder.Services.AddDbContext<DeepSeekCoreContext>(); | |
var app = builder.Build(); | |
// Configure the HTTP request pipeline. | |
if (app.Environment.IsDevelopment()) | |
{ | |
app.UseSwagger(); | |
app.UseSwaggerUI(); | |
} | |
app.UseHttpsRedirection(); | |
app.UseAuthorization(); | |
app.MapControllers(); | |
app.Run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace DeepSeekCore.Entities; | |
public class Supplier | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public string ContactEmail { get; set; } | |
public List<Product> Products { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment