-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from WKLD-Labs/EF-Core
Prepare EF Core with SQLite and made Dummy Model and Controller
- Loading branch information
Showing
9 changed files
with
312 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using csharp_backend; | ||
using csharp_backend.Models; | ||
|
||
namespace csharp_backend.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class DummiesController : ControllerBase | ||
{ | ||
private readonly LabsContext _context; | ||
|
||
public DummiesController(LabsContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
// GET: api/Dummies | ||
[HttpGet] | ||
public async Task<ActionResult<IEnumerable<Dummy>>> GetDummies() | ||
{ | ||
return await _context.Dummies.ToListAsync(); | ||
} | ||
|
||
// GET: api/Dummies/5 | ||
[HttpGet("{id}")] | ||
public async Task<ActionResult<Dummy>> GetDummy(int id) | ||
{ | ||
var dummy = await _context.Dummies.FindAsync(id); | ||
|
||
if (dummy == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return dummy; | ||
} | ||
|
||
// PUT: api/Dummies/5 | ||
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 | ||
[HttpPut("{id}")] | ||
public async Task<IActionResult> PutDummy(int id, Dummy dummy) | ||
{ | ||
if (id != dummy.DummyId) | ||
{ | ||
return BadRequest(); | ||
} | ||
|
||
_context.Entry(dummy).State = EntityState.Modified; | ||
|
||
try | ||
{ | ||
await _context.SaveChangesAsync(); | ||
} | ||
catch (DbUpdateConcurrencyException) | ||
{ | ||
if (!DummyExists(id)) | ||
{ | ||
return NotFound(); | ||
} | ||
else | ||
{ | ||
throw; | ||
} | ||
} | ||
|
||
return NoContent(); | ||
} | ||
|
||
// POST: api/Dummies | ||
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 | ||
[HttpPost] | ||
public async Task<ActionResult<Dummy>> PostDummy(Dummy dummy) | ||
{ | ||
_context.Dummies.Add(dummy); | ||
await _context.SaveChangesAsync(); | ||
|
||
return CreatedAtAction("GetDummy", new { id = dummy.DummyId }, dummy); | ||
} | ||
|
||
// DELETE: api/Dummies/5 | ||
[HttpDelete("{id}")] | ||
public async Task<IActionResult> DeleteDummy(int id) | ||
{ | ||
var dummy = await _context.Dummies.FindAsync(id); | ||
if (dummy == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
_context.Dummies.Remove(dummy); | ||
await _context.SaveChangesAsync(); | ||
|
||
return NoContent(); | ||
} | ||
|
||
private bool DummyExists(int id) | ||
{ | ||
return _context.Dummies.Any(e => e.DummyId == id); | ||
} | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
csharp_backend/Migrations/20240506035454_InitialCreateDummy.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
36 changes: 36 additions & 0 deletions
36
csharp_backend/Migrations/20240506035454_InitialCreateDummy.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
#nullable disable | ||
|
||
namespace csharp_backend.Migrations | ||
{ | ||
/// <inheritdoc /> | ||
public partial class InitialCreateDummy : Migration | ||
{ | ||
/// <inheritdoc /> | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.CreateTable( | ||
name: "Dummies", | ||
columns: table => new | ||
{ | ||
DummyId = table.Column<int>(type: "INTEGER", nullable: false) | ||
.Annotation("Sqlite:Autoincrement", true), | ||
Name = table.Column<string>(type: "TEXT", nullable: false), | ||
Description = table.Column<string>(type: "TEXT", maxLength: 512, nullable: false), | ||
Value = table.Column<string>(type: "TEXT", maxLength: 128, nullable: false) | ||
}, | ||
constraints: table => | ||
{ | ||
table.PrimaryKey("PK_Dummies", x => x.DummyId); | ||
}); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropTable( | ||
name: "Dummies"); | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// <auto-generated /> | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; | ||
using csharp_backend; | ||
|
||
#nullable disable | ||
|
||
namespace csharp_backend.Migrations | ||
{ | ||
[DbContext(typeof(LabsContext))] | ||
partial class LabsContextModelSnapshot : ModelSnapshot | ||
{ | ||
protected override void BuildModel(ModelBuilder modelBuilder) | ||
{ | ||
#pragma warning disable 612, 618 | ||
modelBuilder.HasAnnotation("ProductVersion", "8.0.4"); | ||
|
||
modelBuilder.Entity("csharp_backend.Models.Dummy", b => | ||
{ | ||
b.Property<int>("DummyId") | ||
.ValueGeneratedOnAdd() | ||
.HasColumnType("INTEGER"); | ||
|
||
b.Property<string>("Description") | ||
.IsRequired() | ||
.HasMaxLength(512) | ||
.HasColumnType("TEXT"); | ||
|
||
b.Property<string>("Name") | ||
.IsRequired() | ||
.HasColumnType("TEXT"); | ||
|
||
b.Property<string>("Value") | ||
.IsRequired() | ||
.HasMaxLength(128) | ||
.HasColumnType("TEXT"); | ||
|
||
b.HasKey("DummyId"); | ||
|
||
b.ToTable("Dummies"); | ||
}); | ||
#pragma warning restore 612, 618 | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using csharp_backend.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace csharp_backend | ||
{ | ||
public class LabsContext : DbContext | ||
{ | ||
// Define models here | ||
public DbSet<Dummy> Dummies { get; set; } | ||
|
||
public string DbPath { get; } | ||
|
||
public LabsContext() | ||
{ | ||
var folder = Environment.SpecialFolder.LocalApplicationData; | ||
var path = Environment.GetFolderPath(folder); | ||
path = System.IO.Path.Join(path, ".labsBackend"); | ||
System.IO.Directory.CreateDirectory(path); | ||
DbPath = System.IO.Path.Join(path, "labsData.db"); | ||
} | ||
|
||
// The following configures EF to create a Sqlite database file in the | ||
// special "local" folder for your platform. | ||
protected override void OnConfiguring(DbContextOptionsBuilder options) | ||
=> options.UseSqlite($"Data Source={DbPath}"); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace csharp_backend.Models | ||
{ | ||
public class Dummy | ||
{ | ||
[Key] | ||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] | ||
public int DummyId { get; set; } | ||
|
||
[Required] | ||
public string Name { get; set; } | ||
|
||
[MaxLength(512)] | ||
public string Description { get; set; } | ||
|
||
[Required] | ||
[MaxLength(128)] | ||
public string Value { get; set; } | ||
|
||
} | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# wkldLabs C# ASP.Net Core Web API Backend | ||
Backend for wkldLabs using C# | ||
|
||
## Getting Started with EF Core | ||
This project uses EF Core SQLite for data access. Check out the following guide to get started: [Getting Started with EF Core](https://learn.microsoft.com/en-us/ef/core/get-started/overview/first-app?tabs=netcore-cli) |
This file contains 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