Add project files.

This commit is contained in:
Elias Jansson
2023-01-29 12:04:01 +01:00
parent 1440dc03d5
commit 334c5b02d7
17 changed files with 557 additions and 0 deletions

25
.dockerignore Normal file
View File

@@ -0,0 +1,25 @@
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/azds.yaml
**/bin
**/charts
**/docker-compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

25
Nevyn.sln Normal file
View File

@@ -0,0 +1,25 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.2.32616.157
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Nevyn", "Nevyn\Nevyn.csproj", "{423CD237-7404-4355-868F-CE5861835258}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{423CD237-7404-4355-868F-CE5861835258}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{423CD237-7404-4355-868F-CE5861835258}.Debug|Any CPU.Build.0 = Debug|Any CPU
{423CD237-7404-4355-868F-CE5861835258}.Release|Any CPU.ActiveCfg = Release|Any CPU
{423CD237-7404-4355-868F-CE5861835258}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0A418FE4-56F9-461C-BB0D-C63F1FFD9EFC}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,12 @@
{
"version": 1,
"isRoot": true,
"tools": {
"microsoft.dotnet-msidentity": {
"version": "1.0.3",
"commands": [
"dotnet-msidentity"
]
}
}
}

View File

@@ -0,0 +1,98 @@
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 Nevyn.Models;
namespace Nevyn.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class MoneyController : ControllerBase
{
private readonly MoneyContext _context;
public MoneyController(MoneyContext context)
{
_context = context;
}
// GET: api/ShoppingLists
[HttpGet]
public async Task<ActionResult<IEnumerable<WalletDTO>>> GetWallet()
{
return await _context.UpdateWallet.Select(x => ItemToDTO(x)).ToListAsync();
}
// GET: api/ShoppingLists/5
[HttpGet("{id}")]
public async Task<ActionResult<WalletDTO>> GetWallet(long id)
{
var wallet = await _context.UpdateWallet.FindAsync(id);
if (wallet == null)
{
return NotFound();
}
return ItemToDTO(wallet);
}
// PUT: api/ShoppingLists/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut]
public async Task<IActionResult> PutWallet(long id, Wallet wallet)
{
var currentWallet = await _context.UpdateWallet.FindAsync(wallet.Id);
if (currentWallet == null)
{
return NotFound();
}
currentWallet.Kort = wallet.Kort;
currentWallet.Buffert = wallet.Buffert;
currentWallet.Fonder = wallet.Fonder;
currentWallet.Spara = wallet.Spara;
await _context.SaveChangesAsync();
return NoContent();
}
// POST: api/ShoppingLists
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<WalletDTO>> PostWallet(Wallet inputWallet)
{
var wallet = new Wallet
{
Buffert = inputWallet.Buffert,
Fonder = inputWallet.Fonder,
Kort = inputWallet.Kort,
Spara = inputWallet.Spara,
};
_context.UpdateWallet.Add(wallet);
await _context.SaveChangesAsync();
return CreatedAtAction(
nameof(GetWallet),
new { id = wallet.Id },
ItemToDTO(wallet));
}
private static WalletDTO ItemToDTO(Wallet wallet) =>
new WalletDTO
{
Kort = wallet.Kort,
TotalAmount = wallet.TotalAmount,
AmountLeftPerDay = wallet.MoneyUntilSalary,
Debug = "There is " + wallet.DaysLeftUntilSalary + " days left",
};
}
}

View File

@@ -0,0 +1,127 @@
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 Nevyn.Models;
namespace Nevyn.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ShoppingListsController : ControllerBase
{
private readonly ShoppingContext _context;
public ShoppingListsController(ShoppingContext context)
{
_context = context;
}
// GET: api/ShoppingLists
[HttpGet]
public async Task<ActionResult<IEnumerable<ShoppingListDTO>>> GetUpdateShopping()
{
return await _context.UpdateShopping.Select(x => ItemToDTO(x)).ToListAsync();
}
// GET: api/ShoppingLists/5
[HttpGet("{id}")]
public async Task<ActionResult<ShoppingListDTO>> GetShoppingList(long id)
{
var shoppingList = await _context.UpdateShopping.FindAsync(id);
if (shoppingList == null)
{
return NotFound();
}
return ItemToDTO(shoppingList);
}
// PUT: api/ShoppingLists/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutShoppingList(long id, ShoppingListDTO shoppingListDTO)
{
if (id != shoppingListDTO.Id)
{
return BadRequest();
}
//_context.Entry(shoppingList).State = EntityState.Modified;
var shoppingList = await _context.UpdateShopping.FindAsync(id);
if (shoppingList == null)
{
return NotFound();
}
shoppingList.Name = shoppingListDTO.Name;
shoppingList.IsComplete = shoppingListDTO.IsComplete;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException) when (!ShoppingListExists(id))
{
return NotFound();
}
return NoContent();
}
// POST: api/ShoppingLists
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<ShoppingListDTO>> PostShoppingList(ShoppingListDTO shoppingListDTO)
{
var shoppingList = new ShoppingList
{
IsComplete = shoppingListDTO.IsComplete,
Name = shoppingListDTO.Name
};
_context.UpdateShopping.Add(shoppingList);
await _context.SaveChangesAsync();
return CreatedAtAction(
nameof(GetShoppingList),
new { id = shoppingList.Id },
ItemToDTO(shoppingList));
}
// DELETE: api/ShoppingLists/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteShoppingList(long id)
{
var shoppingList = await _context.UpdateShopping.FindAsync(id);
if (shoppingList == null)
{
return NotFound();
}
_context.UpdateShopping.Remove(shoppingList);
await _context.SaveChangesAsync();
return NoContent();
}
private bool ShoppingListExists(long id)
{
return _context.UpdateShopping.Any(e => e.Id == id);
}
private static ShoppingListDTO ItemToDTO(ShoppingList shoppingList) =>
new ShoppingListDTO
{
Id = shoppingList.Id,
Name = shoppingList.Name,
IsComplete = shoppingList.IsComplete
};
}
}

22
Nevyn/Dockerfile Normal file
View File

@@ -0,0 +1,22 @@
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["Nevyn/Nevyn.csproj", "Nevyn/"]
RUN dotnet restore "Nevyn/Nevyn.csproj"
COPY . .
WORKDIR "/src/Nevyn"
RUN dotnet build "Nevyn.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Nevyn.csproj" -c Release -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Nevyn.dll"]

61
Nevyn/Models/Money.cs Normal file
View File

@@ -0,0 +1,61 @@
using System;
namespace Nevyn.Models
{
public class Wallet
{
public int Id { get; set; }
public int TotalAmount
{
get
{
return this.Kort + this.Buffert + this.Spara + this.Fonder;
}
}
public int Kort { get; set; }
public int Buffert { get; set; }
public int Spara { get; set; }
public int Fonder { get; set; }
public int MoneyUntilSalary {
get
{
DateTime payDay = SalaryCalculator.CalculatePayDay(DateTime.Now);
this.DaysLeftUntilSalary = (payDay - DateTime.Now).Days;
return this.Kort / this.DaysLeftUntilSalary;
}
}
public int DaysLeftUntilSalary { get; set; }
public class SalaryCalculator
{
public static DateTime CalculatePayDay(DateTime date)
{
// Get the 25th of the current month
var payDay = new DateTime(date.Year, date.Month, 25);
// If the 25th is on a weekend, get the previous Friday
if (payDay.DayOfWeek == DayOfWeek.Saturday)
{
payDay = payDay.AddDays(-1);
}
else if (payDay.DayOfWeek == DayOfWeek.Sunday)
{
payDay = payDay.AddDays(-2);
}
return payDay;
}
}
}
public class WalletDTO
{
public int Id { get; set; }
public int TotalAmount { get; set; }
public int AmountLeftPerDay { get; set; }
public int Kort { get; set; }
public string Debug { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
namespace Nevyn.Models
{
public class MoneyContext : DbContext
{
public MoneyContext(DbContextOptions<MoneyContext> options) : base(options)
{
}
public DbSet<Wallet> UpdateWallet { get; set; } = null!;
}
}

View File

@@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
namespace Nevyn.Models
{
public class ShoppingContext : DbContext
{
public ShoppingContext(DbContextOptions<ShoppingContext> options) : base(options)
{
}
public DbSet<ShoppingList> UpdateShopping { get; set; } = null!;
}
}

View File

@@ -0,0 +1,20 @@
namespace Nevyn.Models
{
public class ShoppingList
{
public long Id { get; set; }
public int Amount { get; set; }
public string? Name { get; set; }
public bool IsComplete { get; set; }
public string? Secret { get; set; }
}
public class ShoppingListDTO
{
public long Id { get; set; }
public int Amount { get; set; }
public string? Name { get; set; }
public bool IsComplete { get; set; }
}
}

28
Nevyn/Nevyn.csproj Normal file
View File

@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>aspnet-Nevyn-7C7F87FA-EADD-461B-8418-8C03C2EF8DB3</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.6" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="6.0.6" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.12">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Identity.Web" Version="1.16.0" />
<PackageReference Include="Microsoft.Identity.Web.MicrosoftGraph" Version="1.16.0" />
<PackageReference Include="Microsoft.Identity.Web.UI" Version="1.16.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.15.1" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup>
</Project>

37
Nevyn/Program.cs Normal file
View File

@@ -0,0 +1,37 @@
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Identity.Web;
using Microsoft.EntityFrameworkCore;
using Nevyn.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
//builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddDbContext<ShoppingContext>(opt =>
opt.UseInMemoryDatabase("ShoppingList"));
builder.Services.AddDbContext<MoneyContext>(opt =>
opt.UseInMemoryDatabase("Wallet"));
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
//if (app.Environment.IsDevelopment())
//{
app.UseSwagger();
app.UseSwaggerUI();
//}
//app.UseHttpsRedirection();
//app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();

View File

@@ -0,0 +1,38 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:8080",
"sslPort": 44397
}
},
"profiles": {
"Nevyn": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:8080",
"dotnetRunMessages": true
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"publishAllPorts": true,
"useSSL": true
}
}
}

View File

@@ -0,0 +1,9 @@
{
"dependencies": {
"identityapp1": {
"type": "identityapp",
"connectionId": "AzureAD:ClientSecret",
"dynamicId": null
}
}
}

View File

@@ -0,0 +1,10 @@
{
"dependencies": {
"identityapp1": {
"secretStore": "LocalSecretsFile",
"type": "identityapp.secretStore",
"connectionId": "AzureAD:ClientSecret",
"dynamicId": null
}
}
}

View File

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

9
Nevyn/appsettings.json Normal file
View File

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