Initial commit

This commit is contained in:
2023-01-30 09:20:18 +01:00
commit 1b43dd3109
17 changed files with 662 additions and 0 deletions

80
.gitignore vendored Normal file
View File

@@ -0,0 +1,80 @@
# User-specific files
*.suo
*.user
*.sln.docstates
# Build results
[Dd]ebug/
[Rr]elease/
x64/
[Bb]in/
[Oo]bj/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.log
*.svclog
*.scc
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# Click-Once directory
publish/
# Publish Web Output
*.Publish.xml
*.pubxml
*.azurePubxml
# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
packages/
## TODO: If the tool you use requires repositories.config, also uncomment the next line
!packages/repositories.config
# Windows Azure Build Output
csx/
*.build.csdef
# Others
sql/
*.Cache
ClientBin/
[Ss]tyle[Cc]op.*
![Ss]tyle[Cc]op.targets
~$*
*~
*.dbmdl
*.[Pp]ublish.xml
*.publishsettings
.idea/
*.sqlite3

View File

@@ -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 ThuisApi.Data;
using ThuisApi.Models;
namespace ThuisApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class CardController : ControllerBase
{
private readonly ThuisDbContext _context;
public CardController(ThuisDbContext context)
{
_context = context;
}
// GET: api/Card
[HttpGet]
public async Task<ActionResult<IEnumerable<Card>>> GetCards()
{
return await _context.Cards.ToListAsync();
}
// GET: api/Card/5
[HttpGet("{id}")]
public async Task<ActionResult<Card>> GetCard(int id)
{
var card = await _context.Cards.FindAsync(id);
if (card == null)
{
return NotFound();
}
return card;
}
// PUT: api/Card/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutCard(int id, Card card)
{
if (id != card.CardId)
{
return BadRequest();
}
_context.Entry(card).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!CardExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Card
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<Card>> PostCard(Card card)
{
_context.Cards.Add(card);
await _context.SaveChangesAsync();
return CreatedAtAction("GetCard", new { id = card.CardId }, card);
}
// DELETE: api/Card/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteCard(int id)
{
var card = await _context.Cards.FindAsync(id);
if (card == null)
{
return NotFound();
}
_context.Cards.Remove(card);
await _context.SaveChangesAsync();
return NoContent();
}
private bool CardExists(int id)
{
return _context.Cards.Any(e => e.CardId == id);
}
}
}

View File

@@ -0,0 +1,129 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ThuisApi.Data;
using ThuisApi.Models;
namespace ThuisApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class FreezerController : ControllerBase
{
private readonly ThuisDbContext _context;
private readonly IMapper _mapper;
public FreezerController(ThuisDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
// GET: api/Freezer
[HttpGet]
public async Task<ActionResult<IEnumerable<FreezerDto>>> GetFreezer()
{
// Iterate over the freezers to add amount in freezer.
var freezers = await _context.Freezer.ToListAsync();
var freezerResponse = new List<FreezerDto>();
foreach (var freezer in freezers)
{
freezerResponse.Add(new FreezerDto
{
FreezerId = freezer.FreezerId,
Location = freezer.Name,
AmountInFreezer =
await _context.FreezerItem.CountAsync(a => a.Freezer.FreezerId == freezer.FreezerId)
});
}
return Ok(freezerResponse.ToArray());
}
// GET: api/Freezer/5
[HttpGet("{id}")]
public async Task<ActionResult<FreezerDto>> GetFreezer(int id)
{
var freezer = await _context.Freezer.FindAsync(id);
if (freezer == null)
{
return NotFound();
}
return Ok(_mapper.Map<Freezer, FreezerDto>(freezer));
}
// PUT: api/Freezer/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutFreezer(int id, FreezerDto freezer)
{
if (id != freezer.FreezerId)
{
return BadRequest();
}
_context.Entry(_mapper.Map<FreezerDto, Freezer>(freezer)).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!FreezerExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Freezer
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<FreezerDto>> PostFreezer(FreezerDto freezerDto)
{
var mappedFreezer = _mapper.Map<FreezerDto, Freezer>(freezerDto);
var newFreezer = new Freezer { Name = mappedFreezer.Name };
_context.Freezer.Add(newFreezer);
await _context.SaveChangesAsync();
return CreatedAtAction("GetFreezer", new { id = newFreezer.FreezerId },
_mapper.Map<Freezer, FreezerDto>(newFreezer));
}
// DELETE: api/Freezer/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteFreezer(int id)
{
var freezer = await _context.Freezer.FindAsync(id);
if (freezer == null)
{
return NotFound();
}
_context.Freezer.Remove(freezer);
await _context.SaveChangesAsync();
return NoContent();
}
private bool FreezerExists(int id)
{
return _context.Freezer.Any(e => e.FreezerId == id);
}
}
}

View File

@@ -0,0 +1,136 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using ThuisApi.Data;
using ThuisApi.Models;
namespace ThuisApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class FreezerItemController : ControllerBase
{
private readonly ThuisDbContext _context;
private readonly IMapper _mapper;
public FreezerItemController(ThuisDbContext context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
// GET: api/FreezerItem
[HttpGet]
public async Task<ActionResult<IEnumerable<FreezerItemDto>>> GetFreezerItem()
{
return Ok(_mapper.Map<FreezerItem[], List<FreezerItemDto>>(await _context.FreezerItem.ToArrayAsync()));
}
// GET: api/FreezerItem/InFreezer/2
[HttpGet("InFreezer/{id}")]
public async Task<ActionResult<IEnumerable<FreezerItemDto>>> GetFreezerItemsInFreezer(int id)
{
if (!_context.Freezer.Any(a => a.FreezerId == id))
{
return NotFound();
}
return Ok(_mapper.Map<FreezerItem[], List<FreezerItemDto>>(await _context.FreezerItem
.Where(a => a.FreezerId == id)
.ToArrayAsync()));
}
// GET: api/FreezerItem/5
[HttpGet("{id}")]
public async Task<ActionResult<FreezerItemDto>> GetFreezerItem(int id)
{
var freezerItem = await _context.FreezerItem.FindAsync(id);
if (freezerItem == null)
{
return NotFound();
}
return Ok(_mapper.Map<FreezerItem, FreezerItemDto>(freezerItem));
}
// PUT: api/FreezerItem/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutFreezerItem(int id, FreezerItemDto freezerItem)
{
if (id != freezerItem.FreezerItemId)
{
return BadRequest();
}
_context.Entry(_mapper.Map<FreezerItemDto, FreezerItem>(freezerItem)).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!FreezerItemExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/FreezerItem
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<FreezerItemDto>> PostFreezerItem(FreezerItemDto freezerItemDto)
{
var mappedItem = _mapper.Map<FreezerItemDto, FreezerItem>(freezerItemDto);
if (mappedItem.DatePlacedInFreezer.ToString(CultureInfo.CurrentCulture).Equals(""))
{
mappedItem.DatePlacedInFreezer = DateTime.Now;
}
mappedItem.Freezer =
await _context.Freezer.SingleAsync(freezer => freezer.FreezerId == mappedItem.FreezerId);
_context.FreezerItem.Add(mappedItem);
await _context.SaveChangesAsync();
return CreatedAtAction("GetFreezerItem", new { id = mappedItem.FreezerItemId },
_mapper.Map<FreezerItem, FreezerItemDto>(mappedItem));
}
// DELETE: api/FreezerItem/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteFreezerItem(int id)
{
var freezerItem = await _context.FreezerItem.FindAsync(id);
if (freezerItem == null)
{
return NotFound();
}
_context.FreezerItem.Remove(freezerItem);
await _context.SaveChangesAsync();
return NoContent();
}
private bool FreezerItemExists(int id)
{
return _context.FreezerItem.Any(e => e.FreezerItemId == id);
}
}
}

32
Data/ThuisDbContext.cs Normal file
View File

@@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore;
using ThuisApi.Models;
namespace ThuisApi.Data;
public class ThuisDbContext : DbContext
{
public DbSet<Card> Cards { get; set; }
public DbSet<Freezer> Freezer { get; set; }
public DbSet<FreezerItem> FreezerItem { get; set; }
public ThuisDbContext(DbContextOptions<ThuisDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Freezer>().HasData(
new Freezer
{
FreezerId = 1,
Name = "Keuken"
},
new Freezer
{
FreezerId = 2,
Name = "Berging"
});
}
}

9
Models/Card.cs Normal file
View File

@@ -0,0 +1,9 @@
namespace ThuisApi.Models
{
public class Card
{
public int CardId { get; set; }
public string Issuer { get; set; }
public string Code { get; set; }
}
}

7
Models/Freezer.cs Normal file
View File

@@ -0,0 +1,7 @@
namespace ThuisApi.Models;
public class Freezer
{
public int FreezerId { get; set; }
public string Name { get; set; }
}

8
Models/FreezerDto.cs Normal file
View File

@@ -0,0 +1,8 @@
namespace ThuisApi.Models;
public class FreezerDto
{
public int FreezerId { get; set; }
public string Location { get; set; }
public int AmountInFreezer { get; set; }
}

13
Models/FreezerItem.cs Normal file
View File

@@ -0,0 +1,13 @@
namespace ThuisApi.Models;
public class FreezerItem
{
public int FreezerItemId { get; set; }
public string Item { get; set; }
public int Amount { get; set; }
public int Drawer { get; set; }
public DateTime DatePlacedInFreezer { get; set; }
public int FreezerId { get; set; }
public Freezer Freezer { get; set; }
}

11
Models/FreezerItemDto.cs Normal file
View File

@@ -0,0 +1,11 @@
namespace ThuisApi.Models;
public class FreezerItemDto
{
public int FreezerItemId { get; set; }
public string Item { get; set; }
public int Amount { get; set; }
public int Drawer { get; set; }
public string DateTimeAdded { get; set; }
public int FreezerId { get; set; }
}

18
Models/ThuisApiProfile.cs Normal file
View File

@@ -0,0 +1,18 @@
using AutoMapper;
namespace ThuisApi.Models;
public class ThuisApiProfile : Profile
{
public ThuisApiProfile()
{
CreateMap<Freezer, FreezerDto>();
CreateMap<FreezerDto, Freezer>()
.ForMember(dest => dest.FreezerId, opt => opt.Ignore())
.ForSourceMember(src => src.AmountInFreezer, opt => opt.DoNotValidate());
CreateMap<FreezerItem, FreezerItemDto>();
CreateMap<FreezerItemDto, FreezerItem>()
// .ForSourceMember(src => src.Freezer, opt => opt.DoNotValidate())
.ForMember(dest => dest.Freezer, opt => opt.Ignore());
}
}

36
Program.cs Normal file
View File

@@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore;
using ThuisApi.Data;
using ThuisApi.Models;
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<ThuisDbContext>(options =>
{
options.UseSqlite("Data Source=db.sqlite3");
});
builder.Services.AddAutoMapper(typeof(ThuisApiProfile));
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();

View File

@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:33778",
"sslPort": 44391
}
},
"profiles": {
"ThuisApi": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": false,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7239;http://localhost:5052",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": false,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

27
ThuisApi.csproj Normal file
View File

@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="12.0.0" />
<PackageReference Include="AutoMapper.EF6" Version="2.1.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.7" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.10" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"Microsoft.AspNetCore": "Warning"
}
}
}

9
appsettings.json Normal file
View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}

BIN
test.pfx Normal file

Binary file not shown.