diff --git a/.gitignore b/.gitignore index ab9ce1f..48597ec 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ # Created by https://www.toptal.com/developers/gitignore/api/dotnetcore,visualstudio,visualstudiocode # Edit at https://www.toptal.com/developers/gitignore?templates=dotnetcore,visualstudio,visualstudiocode +log.txt + ### DotnetCore ### # .NET Core build folders bin/ diff --git a/cat_cafe/.config/dotnet-tools.json b/cat_cafe/.config/dotnet-tools.json new file mode 100644 index 0000000..3ba0fe6 --- /dev/null +++ b/cat_cafe/.config/dotnet-tools.json @@ -0,0 +1,12 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "dotnet-ef": { + "version": "6.0.0", + "commands": [ + "dotnet-ef" + ] + } + } +} \ No newline at end of file diff --git a/cat_cafe/Controllers/BarsController.cs b/cat_cafe/Controllers/BarsController.cs index 26b907d..1a8b202 100644 --- a/cat_cafe/Controllers/BarsController.cs +++ b/cat_cafe/Controllers/BarsController.cs @@ -14,9 +14,9 @@ namespace cat_cafe.Controllers [ApiController] public class BarsController : ControllerBase { - private readonly BarContext _context; + private readonly CatCafeDbContext _context; - public BarsController(BarContext context) + public BarsController(CatCafeDbContext context) { _context = context; } @@ -25,14 +25,14 @@ namespace cat_cafe.Controllers [HttpGet] public async Task>> GetBars() { - return await _context.Bars.ToListAsync(); + return await _context.bars.ToListAsync(); } // GET: api/Bars/5 [HttpGet("{id}")] public async Task> GetBar(long id) { - var bar = await _context.Bars.FindAsync(id); + var bar = await _context.bars.FindAsync(id); if (bar == null) { @@ -78,7 +78,7 @@ namespace cat_cafe.Controllers [HttpPost] public async Task> PostBar(Bar bar) { - _context.Bars.Add(bar); + _context.bars.Add(bar); await _context.SaveChangesAsync(); return CreatedAtAction("GetBar", new { id = bar.Id }, bar); @@ -88,13 +88,13 @@ namespace cat_cafe.Controllers [HttpDelete("{id}")] public async Task DeleteBar(long id) { - var bar = await _context.Bars.FindAsync(id); + var bar = await _context.bars.FindAsync(id); if (bar == null) { return NotFound(); } - _context.Bars.Remove(bar); + _context.bars.Remove(bar); await _context.SaveChangesAsync(); return NoContent(); @@ -102,7 +102,7 @@ namespace cat_cafe.Controllers private bool BarExists(long id) { - return _context.Bars.Any(e => e.Id == id); + return _context.bars.Any(e => e.Id == id); } } } diff --git a/cat_cafe/Controllers/CatsController.cs b/cat_cafe/Controllers/CatsController.cs index b6f4160..be013cb 100644 --- a/cat_cafe/Controllers/CatsController.cs +++ b/cat_cafe/Controllers/CatsController.cs @@ -16,10 +16,10 @@ namespace cat_cafe.Controllers [ApiController] public class CatsController : ControllerBase { - private readonly CatContext _context; + private readonly CatCafeDbContext _context; private readonly IMapper _mapper; - public CatsController(CatContext context, IMapper mapper) + public CatsController(CatCafeDbContext context, IMapper mapper) { _mapper = mapper; _context = context; @@ -29,7 +29,7 @@ namespace cat_cafe.Controllers [HttpGet] public async Task>> GetCats() { - var cats = await _context.Cats.ToListAsync(); + var cats = await _context.cats.ToListAsync(); return _mapper.Map>(cats); } @@ -38,7 +38,7 @@ namespace cat_cafe.Controllers [HttpGet("{id}")] public async Task> GetCat(long id) { - var cat = await _context.Cats.FindAsync(id); + var cat = await _context.cats.FindAsync(id); if (cat == null) { @@ -86,7 +86,7 @@ namespace cat_cafe.Controllers public async Task> PostCat(CatDto catDto) { Cat cat = _mapper.Map(catDto); - _context.Cats.Add(cat); + _context.cats.Add(cat); await _context.SaveChangesAsync(); return CreatedAtAction("GetCat", new { id = catDto.Id }, _mapper.Map(cat)); @@ -96,13 +96,13 @@ namespace cat_cafe.Controllers [HttpDelete("{id}")] public async Task DeleteCat(long id) { - var cat = await _context.Cats.FindAsync(id); + var cat = await _context.cats.FindAsync(id); if (cat == null) { return NotFound(); } - _context.Cats.Remove(cat); + _context.cats.Remove(cat); await _context.SaveChangesAsync(); return NoContent(); @@ -110,7 +110,7 @@ namespace cat_cafe.Controllers private bool CatExists(long id) { - return _context.Cats.Any(e => e.Id == id); + return _context.cats.Any(e => e.Id == id); } } } diff --git a/cat_cafe/Controllers/CustomersController.cs b/cat_cafe/Controllers/CustomersController.cs index e2a5b21..dfb4f73 100644 --- a/cat_cafe/Controllers/CustomersController.cs +++ b/cat_cafe/Controllers/CustomersController.cs @@ -18,11 +18,11 @@ namespace cat_cafe.Controllers [ApiController] public class CustomersController : ControllerBase { - private readonly CustomerContext _context; + private readonly CatCafeDbContext _context; private readonly IMapper _mapper; private readonly ILogger _logger; - public CustomersController(CustomerContext context,IMapper mapper,ILogger logger) + public CustomersController(CatCafeDbContext context,IMapper mapper,ILogger logger) { _context = context; _mapper = mapper; @@ -34,8 +34,9 @@ namespace cat_cafe.Controllers public async Task>> GetCustomers() { Log.Information(this.Request.Method + " => get All customers"); + _logger.LogInformation("hello"); - var customers = await _context.Customers.ToListAsync(); + var customers = await _context.customers.ToListAsync(); Log.Information(this.Request.Method + " => " + this.Response.StatusCode.ToString() + " " @@ -49,7 +50,7 @@ namespace cat_cafe.Controllers public async Task> GetCustomer(long id) { Log.Information(this.Request.Method + " => get by ID {@id}",id); - var customer = await _context.Customers.FindAsync(id); + var customer = await _context.customers.FindAsync(id); if (customer == null) { @@ -112,7 +113,7 @@ namespace cat_cafe.Controllers Log.Information(this.Request.Method + " => post customer"); Customer customer = _mapper.Map(customerDto); - _context.Customers.Add(customer); + _context.customers.Add(customer); await _context.SaveChangesAsync(); Log.Information(this.Request.Method + " => " @@ -129,14 +130,14 @@ namespace cat_cafe.Controllers { Log.Information(this.Request.Method + " => delete by ID {@id}", id); - var customer = await _context.Customers.FindAsync(id); + var customer = await _context.customers.FindAsync(id); if (customer == null) { Log.Information(this.Request.Method + " => " + NotFound().StatusCode.ToString()); return NotFound(); } - _context.Customers.Remove(customer); + _context.customers.Remove(customer); await _context.SaveChangesAsync(); Log.Information(this.Request.Method + " => " @@ -149,7 +150,7 @@ namespace cat_cafe.Controllers private bool CustomerExists(long id) { - return _context.Customers.Any(e => e.Id == id); + return _context.customers.Any(e => e.Id == id); } } } diff --git a/cat_cafe/EFCatCafe.db b/cat_cafe/EFCatCafe.db new file mode 100644 index 0000000..5174413 Binary files /dev/null and b/cat_cafe/EFCatCafe.db differ diff --git a/cat_cafe/Migrations/20230204085149_myMigrationNa.Designer.cs b/cat_cafe/Migrations/20230204085149_myMigrationNa.Designer.cs new file mode 100644 index 0000000..1fb7e45 --- /dev/null +++ b/cat_cafe/Migrations/20230204085149_myMigrationNa.Designer.cs @@ -0,0 +1,90 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using cat_cafe.Repositories; + +#nullable disable + +namespace cat_cafe.Migrations +{ + [DbContext(typeof(CatCafeDbContext))] + [Migration("20230204085149_myMigrationNa")] + partial class myMigrationNa + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "6.0.12"); + + modelBuilder.Entity("cat_cafe.Entities.Bar", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Name") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("bars"); + }); + + modelBuilder.Entity("cat_cafe.Entities.Cat", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Age") + .HasColumnType("INTEGER"); + + b.Property("BarId") + .HasColumnType("INTEGER"); + + b.Property("Name") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("BarId"); + + b.ToTable("cats"); + }); + + modelBuilder.Entity("cat_cafe.Entities.Customer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Age") + .HasColumnType("INTEGER"); + + b.Property("FullName") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("customers"); + }); + + modelBuilder.Entity("cat_cafe.Entities.Cat", b => + { + b.HasOne("cat_cafe.Entities.Bar", null) + .WithMany("cats") + .HasForeignKey("BarId"); + }); + + modelBuilder.Entity("cat_cafe.Entities.Bar", b => + { + b.Navigation("cats"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/cat_cafe/Migrations/20230204085149_myMigrationNa.cs b/cat_cafe/Migrations/20230204085149_myMigrationNa.cs new file mode 100644 index 0000000..5ee0387 --- /dev/null +++ b/cat_cafe/Migrations/20230204085149_myMigrationNa.cs @@ -0,0 +1,76 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace cat_cafe.Migrations +{ + public partial class myMigrationNa : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "bars", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Name = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_bars", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "customers", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + FullName = table.Column(type: "TEXT", nullable: false), + Age = table.Column(type: "INTEGER", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_customers", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "cats", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Name = table.Column(type: "TEXT", nullable: true), + Age = table.Column(type: "INTEGER", nullable: false), + BarId = table.Column(type: "INTEGER", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_cats", x => x.Id); + table.ForeignKey( + name: "FK_cats_bars_BarId", + column: x => x.BarId, + principalTable: "bars", + principalColumn: "Id"); + }); + + migrationBuilder.CreateIndex( + name: "IX_cats_BarId", + table: "cats", + column: "BarId"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "cats"); + + migrationBuilder.DropTable( + name: "customers"); + + migrationBuilder.DropTable( + name: "bars"); + } + } +} diff --git a/cat_cafe/Migrations/CatCafeDbContextModelSnapshot.cs b/cat_cafe/Migrations/CatCafeDbContextModelSnapshot.cs new file mode 100644 index 0000000..be2c397 --- /dev/null +++ b/cat_cafe/Migrations/CatCafeDbContextModelSnapshot.cs @@ -0,0 +1,88 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using cat_cafe.Repositories; + +#nullable disable + +namespace cat_cafe.Migrations +{ + [DbContext(typeof(CatCafeDbContext))] + partial class CatCafeDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "6.0.12"); + + modelBuilder.Entity("cat_cafe.Entities.Bar", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Name") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("bars"); + }); + + modelBuilder.Entity("cat_cafe.Entities.Cat", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Age") + .HasColumnType("INTEGER"); + + b.Property("BarId") + .HasColumnType("INTEGER"); + + b.Property("Name") + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("BarId"); + + b.ToTable("cats"); + }); + + modelBuilder.Entity("cat_cafe.Entities.Customer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Age") + .HasColumnType("INTEGER"); + + b.Property("FullName") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("customers"); + }); + + modelBuilder.Entity("cat_cafe.Entities.Cat", b => + { + b.HasOne("cat_cafe.Entities.Bar", null) + .WithMany("cats") + .HasForeignKey("BarId"); + }); + + modelBuilder.Entity("cat_cafe.Entities.Bar", b => + { + b.Navigation("cats"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/cat_cafe/Program.cs b/cat_cafe/Program.cs index e39c7e3..fb61f53 100644 --- a/cat_cafe/Program.cs +++ b/cat_cafe/Program.cs @@ -2,6 +2,9 @@ using Microsoft.EntityFrameworkCore; using cat_cafe.Repositories; using Serilog; using Serilog.Sinks.File; +using System.Diagnostics; +using cat_cafe.Entities; + var builder = WebApplication.CreateBuilder(args); @@ -15,12 +18,26 @@ Log.Logger = new LoggerConfiguration().MinimumLevel.Information().WriteTo.File(" // Add services to the container. builder.Services.AddControllers(); -builder.Services.AddDbContext(opt => -opt.UseInMemoryDatabase("CatCafe")); -builder.Services.AddDbContext(opt => -opt.UseSqlite("CatCafe")); -builder.Services.AddDbContext(opt => -opt.UseInMemoryDatabase("CatCafe")); +builder.Services.AddDbContext(); +//builder.Services.AddDbContext(opt => +//opt.UseInMemoryDatabase("CatCafe")); +//builder.Services.AddDbContext(opt => +//opt.UseSqlite("CatCafe")); +//builder.Services.AddDbContext(opt => +//opt.UseSqlite("$Data Source ={CatCafe}")); + +try +{ + // DB stuff when the app opens + using (CatCafeDbContext db = new()) + { + + } +} +catch (Exception ex) { Console.WriteLine($"{ex.Message}\n... Couldn't use the database"); } + + + // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); diff --git a/cat_cafe/Repositories/BarContext.cs b/cat_cafe/Repositories/BarContext.cs index f0f4672..fbf8826 100644 --- a/cat_cafe/Repositories/BarContext.cs +++ b/cat_cafe/Repositories/BarContext.cs @@ -1,17 +1,17 @@ -using cat_cafe.Entities; -using Microsoft.EntityFrameworkCore; -using System; -namespace cat_cafe.Repositories -{ - public class BarContext : DbContext - { - public BarContext(DbContextOptions options) - : base(options) - { - } +//using cat_cafe.Entities; +//using Microsoft.EntityFrameworkCore; +//using System; +//namespace cat_cafe.Repositories +//{ +// public class BarContext : DbContext +// { +// public BarContext(DbContextOptions options) +// : base(options) +// { +// } - public DbSet Bars { get; set; } = null!; +// public DbSet Bars { get; set; } = null!; - } -} +// } +//} diff --git a/cat_cafe/Repositories/CatCafeDbContext.cs b/cat_cafe/Repositories/CatCafeDbContext.cs new file mode 100644 index 0000000..0d7bec9 --- /dev/null +++ b/cat_cafe/Repositories/CatCafeDbContext.cs @@ -0,0 +1,33 @@ +using System; +using Microsoft.EntityFrameworkCore; +using cat_cafe.Entities; + +namespace cat_cafe.Repositories +{ + public class CatCafeDbContext:DbContext + { + + public DbSet customers { get; set; } + public DbSet bars { get; set; } + public DbSet cats { get; set; } + + + public CatCafeDbContext(DbContextOptions opts) + :base(opts) + { + Database.EnsureCreated(); + } + + public CatCafeDbContext() + { + } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (!optionsBuilder.IsConfigured) optionsBuilder.UseSqlite("Data Source=EFCatCafe.db").EnableSensitiveDataLogging(); + } + + + } +} + diff --git a/cat_cafe/Repositories/CatContext.cs b/cat_cafe/Repositories/CatContext.cs index f0ca879..d8253a9 100644 --- a/cat_cafe/Repositories/CatContext.cs +++ b/cat_cafe/Repositories/CatContext.cs @@ -1,16 +1,16 @@ -using cat_cafe.Entities; -using Microsoft.EntityFrameworkCore; +//using cat_cafe.Entities; +//using Microsoft.EntityFrameworkCore; -namespace cat_cafe.Repositories -{ - public class CatContext : DbContext - { - public CatContext(DbContextOptions options) - : base(options) - { - } +//namespace cat_cafe.Repositories +//{ +// public class CatContext : DbContext +// { +// public CatContext(DbContextOptions options) +// : base(options) +// { +// } - public DbSet Cats { get; set; } = null!; +// public DbSet Cats { get; set; } = null!; - } -} +// } +//} diff --git a/cat_cafe/Repositories/CustomerContext.cs b/cat_cafe/Repositories/CustomerContext.cs index a8f9406..dba5927 100644 --- a/cat_cafe/Repositories/CustomerContext.cs +++ b/cat_cafe/Repositories/CustomerContext.cs @@ -1,28 +1,33 @@ -using System; -using cat_cafe.Entities; -using Microsoft.EntityFrameworkCore; -namespace cat_cafe.Repositories -{ - public class CustomerContext:DbContext - { +//using System; +//using cat_cafe.Entities; +//using Microsoft.EntityFrameworkCore; +//namespace cat_cafe.Repositories +//{ +// public class CustomerContext:DbContext +// { - public readonly IConfiguration configuration; +// public readonly IConfiguration configuration; - public CustomerContext(IConfiguration _configuration) - { - configuration = _configuration; - } +// public string DbPath { get; } - protected override void OnConfiguring(DbContextOptionsBuilder opts) - { - opts.UseSqlite(configuration.GetConnectionString(@"Data Source=CatCafe.db")); - } +// public CustomerContext(IConfiguration _configuration) +// { +// configuration = _configuration; +// var folder = Environment.SpecialFolder.LocalApplicationData; +// var path = Environment.GetFolderPath(folder); +// DbPath = System.IO.Path.Join(path, "CatCafe.db"); +// } +// protected override void OnConfiguring(DbContextOptionsBuilder opts) +// { +// opts.UseSqlite($"Data Source=DbPath.db"); +// } - /*public CustomerContext(DbContextOptions options) - : base(options) - { }*/ - public DbSet Customers { get; set; } = null!; - } -} + +// /*public CustomerContext(DbContextOptions options) +// : base(options) +// { }*/ +// public DbSet Customers { get; set; } = null!; +// } +//} diff --git a/cat_cafe/cat_cafe.csproj b/cat_cafe/cat_cafe.csproj index 31cfb39..2cb07ad 100644 --- a/cat_cafe/cat_cafe.csproj +++ b/cat_cafe/cat_cafe.csproj @@ -4,10 +4,10 @@ net6.0 enable enable + $(MSBuildProjectDirectory) - @@ -27,5 +27,6 @@ + diff --git a/cat_cafe/log.txt b/cat_cafe/log.txt index a524769..b5765e5 100644 --- a/cat_cafe/log.txt +++ b/cat_cafe/log.txt @@ -105,3 +105,35 @@ 2023-01-21 08:51:20.382 +01:00 [INF] program start 2023-01-21 09:52:51.283 +01:00 [INF] program start 2023-01-21 09:53:00.966 +01:00 [INF] POST => post customer +2023-01-28 10:43:30.841 +01:00 [INF] program start +2023-01-28 10:43:40.549 +01:00 [INF] GET => get All customers +2023-01-28 10:44:16.773 +01:00 [INF] program start +2023-01-28 10:44:22.429 +01:00 [INF] GET => get All customers +2023-01-28 10:54:58.453 +01:00 [INF] program start +2023-01-28 10:55:31.556 +01:00 [INF] program start +2023-02-04 08:16:09.059 +01:00 [INF] program start +2023-02-04 08:17:13.242 +01:00 [INF] program start +2023-02-04 08:33:04.111 +01:00 [INF] program start +2023-02-04 08:33:11.901 +01:00 [INF] GET => get All customers +2023-02-04 09:34:41.222 +01:00 [INF] program start +2023-02-04 09:38:43.323 +01:00 [INF] program start +2023-02-04 09:44:32.881 +01:00 [INF] program start +2023-02-04 09:44:44.716 +01:00 [INF] GET => get All customers +2023-02-04 09:45:51.387 +01:00 [INF] program start +2023-02-04 09:45:58.688 +01:00 [INF] GET => get All customers +2023-02-04 09:51:59.042 +01:00 [INF] program start +2023-02-04 09:52:06.882 +01:00 [INF] GET => get All customers +2023-02-04 09:52:58.838 +01:00 [INF] program start +2023-02-04 09:53:07.179 +01:00 [INF] GET => get All customers +2023-02-04 09:53:07.239 +01:00 [INF] GET => 200 System.Collections.Generic.List`1[cat_cafe.Entities.Customer] length[0] +2023-02-04 09:53:21.763 +01:00 [INF] POST => post customer +2023-02-04 09:53:21.869 +01:00 [INF] POST => 201 cat_cafe.Entities.Customer {"Id":1,"FullName":"string","Age":0} +2023-02-04 09:53:25.604 +01:00 [INF] POST => post customer +2023-02-04 09:53:25.608 +01:00 [INF] POST => 201 cat_cafe.Entities.Customer {"Id":2,"FullName":"string","Age":0} +2023-02-04 09:53:28.472 +01:00 [INF] POST => post customer +2023-02-04 09:53:28.478 +01:00 [INF] POST => 201 cat_cafe.Entities.Customer {"Id":3,"FullName":"string","Age":0} +2023-02-04 09:53:32.476 +01:00 [INF] GET => get All customers +2023-02-04 09:53:32.486 +01:00 [INF] GET => 200 System.Collections.Generic.List`1[cat_cafe.Entities.Customer] length[3] +2023-02-04 09:53:40.756 +01:00 [INF] program start +2023-02-04 09:53:50.214 +01:00 [INF] GET => get All customers +2023-02-04 09:53:50.290 +01:00 [INF] GET => 200 System.Collections.Generic.List`1[cat_cafe.Entities.Customer] length[3]