implemented basic crud logic; NB: CONNECTION STRING IN PROGRAM.CS IS EMPTY

This commit is contained in:
Gleb Lagov 2022-11-19 11:10:00 +03:00
parent 400f717f97
commit 32b5d16c44
14 changed files with 313 additions and 58 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

View File

@ -4,9 +4,16 @@
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>90bdd6c9-a32c-40b9-ada1-b97ee2997ef2</UserSecretsId>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="AspNet.Security.OAuth.Vkontakte" Version="6.0.14" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="6.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.11" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.7" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
</ItemGroup> </ItemGroup>

View File

@ -0,0 +1,53 @@
using Backend.Api.Data;
using Backend.Api.Data.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Backend.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
private readonly UserService _userService;
public UsersController(UserService userService)
{
_userService = userService;
}
[HttpPost("Add")]
public ActionResult Add(User user)
{
_userService.Add(user);
return Ok(user);
}
[HttpDelete("Remove")]
public ActionResult Remove(long id)
{
_userService.Remove(id);
return Ok();
}
[HttpPatch("Update")]
public ActionResult Update(long id, User user)
{
_userService.Update(id, user);
return Ok(user);
}
[HttpGet("Get")]
public ActionResult GetById(long id)
{
var user = _userService.Get(id);
return Ok(user);
}
[HttpGet("GetVehicles")]
public ActionResult GetVehicles(long id)
{
var vehicles = _userService.GetVehicles(id);
return Ok(vehicles);
}
}
}

View File

@ -0,0 +1,53 @@
using Backend.Api.Data;
using Backend.Api.Data.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Backend.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class VehiclesController : ControllerBase
{
private VehicleService _vehicleService;
public VehiclesController(VehicleService vehicleService)
{
_vehicleService = vehicleService;
}
[HttpPost("Add")]
public ActionResult Add(Vehicle vehicle)
{
_vehicleService.Add(vehicle);
return Ok(vehicle);
}
[HttpDelete("Remove")]
public ActionResult Remove(long id)
{
_vehicleService.Remove(id);
return Ok();
}
[HttpPatch("Update")]
public ActionResult Update(long id, Vehicle vehicle)
{
_vehicleService.Update(id, vehicle);
return Ok(vehicle);
}
[HttpGet("Get")]
public ActionResult GetById(long id)
{
var vehicle = _vehicleService.Get(id);
return Ok(vehicle);
}
[HttpGet("GetOwner")]
public ActionResult GetOwner(long id)
{
var owner = _vehicleService.GetOwner(id);
return Ok(owner);
}
}
}

View File

@ -1,32 +0,0 @@
using Microsoft.AspNetCore.Mvc;
namespace Backend.Api.Controllers;
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
})
.ToArray();
}
}

View File

@ -0,0 +1,14 @@
using Microsoft.EntityFrameworkCore;
namespace Backend.Api.Data
{
public class DataContext : DbContext
{
public DataContext(DbContextOptions<DataContext> options) : base(options)
{
}
public DbSet<User> Users { get; set; }
public DbSet<Vehicle> Vehicles { get; set; }
}
}

View File

@ -0,0 +1,47 @@
namespace Backend.Api.Data.Services
{
public class UserService
{
private readonly DataContext _context;
public UserService(DataContext context)
{
_context = context;
}
public void Add(User user)
{
_context.Users.Add(user);
_context.SaveChanges();
}
public void Remove(long id)
{
var user = _context.Users.FirstOrDefault(x => x.Id == id);
if (user != null)
{
_context.Users.Remove(user);
_context.SaveChanges();
}
}
public void Update(long id, User newUser)
{
var oldUser = _context.Users.FirstOrDefault(x =>x.Id == id);
oldUser = newUser;
_context.SaveChanges();
}
public User? Get(long id)
{
var user = _context.Users.FirstOrDefault(x => x.Id == id);
return user;
}
public List<Vehicle> GetVehicles(long id)
{
var userId = _context.Users.FirstOrDefault(x => x.Id == id).Id;
var vehicles = _context.Vehicles.Where(x => x.OwnerId == userId).ToList<Vehicle>();
return vehicles;
}
}
}

View File

@ -0,0 +1,45 @@
namespace Backend.Api.Data.Services
{
public class VehicleService
{
private readonly DataContext _context;
public VehicleService(DataContext context) {
_context = context;
}
public void Add(Vehicle vehicle)
{
_context.Vehicles.Add(vehicle);
_context.SaveChanges();
}
public void Remove(long id)
{
var vehicle = _context.Vehicles.FirstOrDefault(x => x.Id == id);
if (vehicle != null)
{
_context.Vehicles.Remove(vehicle);
_context.SaveChanges();
}
}
public void Update(long id, Vehicle newVehicle)
{
var oldVehicle = _context.Vehicles.FirstOrDefault(x => x.Id == id);
oldVehicle = newVehicle;
_context.SaveChanges();
}
public Vehicle? Get(long id)
{
var vehicle = _context.Vehicles.FirstOrDefault(x => x.Id == id);
return vehicle;
}
public User? GetOwner(long id)
{
var ownerId = _context.Vehicles.FirstOrDefault(x => x.Id == id).OwnerId;
return _context.Users.FirstOrDefault(x => x.Id == ownerId);
}
}
}

9
Backend.Api/Data/User.cs Normal file
View File

@ -0,0 +1,9 @@
namespace Backend.Api.Data
{
public class User
{
public long Id { get; set; }
public string Name { get; set; } = "Undefined name";
public string Surname { get; set; } = "Undefined surname";
}
}

View File

@ -0,0 +1,9 @@
namespace Backend.Api.Data
{
public class Vehicle
{
public long Id { get; set; }
public long OwnerId { get; set; }
public string PlateNumber { get; set; } = "Undefined plate number";
}
}

22
Backend.Api/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 ["Backend.Api/Backend.Api.csproj", "Backend.Api/"]
RUN dotnet restore "Backend.Api/Backend.Api.csproj"
COPY . .
WORKDIR "/src/Backend.Api"
RUN dotnet build "Backend.Api.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "Backend.Api.csproj" -c Release -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Backend.Api.dll"]

View File

@ -1,3 +1,8 @@
using Backend.Api.Data;
using Microsoft.AspNetCore.Authentication.Google;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
@ -5,8 +10,11 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers(); builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(); builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<DataContext>(options => options.UseNpgsql(""));
var app = builder.Build(); var app = builder.Build();
// Configure the HTTP request pipeline. // Configure the HTTP request pipeline.

View File

@ -1,23 +1,14 @@
{ {
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:52732",
"sslPort": 44361
}
},
"profiles": { "profiles": {
"Backend.Api": { "Backend.Api": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "swagger", "launchUrl": "swagger",
"applicationUrl": "https://localhost:7070;http://localhost:5297",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} },
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7070;http://localhost:5297"
}, },
"IIS Express": { "IIS Express": {
"commandName": "IISExpress", "commandName": "IISExpress",
@ -26,6 +17,22 @@
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }
},
"Docker": {
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"publishAllPorts": true,
"useSSL": true
}
},
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:52732",
"sslPort": 44361
} }
} }
} }

View File

@ -1,12 +0,0 @@
namespace Backend.Api;
public class WeatherForecast
{
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string? Summary { get; set; }
}