From 32b5d16c44bac16a20ca3a129c26fde709226ccc Mon Sep 17 00:00:00 2001 From: Gleb Lagov Date: Sat, 19 Nov 2022 11:10:00 +0300 Subject: [PATCH] implemented basic crud logic; NB: CONNECTION STRING IN PROGRAM.CS IS EMPTY --- .dockerignore | 25 +++++++++ Backend.Api/Backend.Api.csproj | 7 +++ Backend.Api/Controllers/UsersController.cs | 53 +++++++++++++++++++ Backend.Api/Controllers/VehiclesController.cs | 53 +++++++++++++++++++ .../Controllers/WeatherForecastController.cs | 32 ----------- Backend.Api/Data/DataContext.cs | 14 +++++ Backend.Api/Data/Services/UserService.cs | 47 ++++++++++++++++ Backend.Api/Data/Services/VehicleService.cs | 45 ++++++++++++++++ Backend.Api/Data/User.cs | 9 ++++ Backend.Api/Data/Vehicle.cs | 9 ++++ Backend.Api/Dockerfile | 22 ++++++++ Backend.Api/Program.cs | 8 +++ Backend.Api/Properties/launchSettings.json | 35 +++++++----- Backend.Api/WeatherForecast.cs | 12 ----- 14 files changed, 313 insertions(+), 58 deletions(-) create mode 100644 .dockerignore create mode 100644 Backend.Api/Controllers/UsersController.cs create mode 100644 Backend.Api/Controllers/VehiclesController.cs delete mode 100644 Backend.Api/Controllers/WeatherForecastController.cs create mode 100644 Backend.Api/Data/DataContext.cs create mode 100644 Backend.Api/Data/Services/UserService.cs create mode 100644 Backend.Api/Data/Services/VehicleService.cs create mode 100644 Backend.Api/Data/User.cs create mode 100644 Backend.Api/Data/Vehicle.cs create mode 100644 Backend.Api/Dockerfile delete mode 100644 Backend.Api/WeatherForecast.cs diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3729ff0 --- /dev/null +++ b/.dockerignore @@ -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 \ No newline at end of file diff --git a/Backend.Api/Backend.Api.csproj b/Backend.Api/Backend.Api.csproj index 60bf9ea..142d4c5 100644 --- a/Backend.Api/Backend.Api.csproj +++ b/Backend.Api/Backend.Api.csproj @@ -4,9 +4,16 @@ net6.0 enable enable + 90bdd6c9-a32c-40b9-ada1-b97ee2997ef2 + Linux + + + + + diff --git a/Backend.Api/Controllers/UsersController.cs b/Backend.Api/Controllers/UsersController.cs new file mode 100644 index 0000000..792dfe7 --- /dev/null +++ b/Backend.Api/Controllers/UsersController.cs @@ -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); + } + } +} diff --git a/Backend.Api/Controllers/VehiclesController.cs b/Backend.Api/Controllers/VehiclesController.cs new file mode 100644 index 0000000..d4b10e1 --- /dev/null +++ b/Backend.Api/Controllers/VehiclesController.cs @@ -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); + } + } +} diff --git a/Backend.Api/Controllers/WeatherForecastController.cs b/Backend.Api/Controllers/WeatherForecastController.cs deleted file mode 100644 index 42b7a67..0000000 --- a/Backend.Api/Controllers/WeatherForecastController.cs +++ /dev/null @@ -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 _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable 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(); - } -} diff --git a/Backend.Api/Data/DataContext.cs b/Backend.Api/Data/DataContext.cs new file mode 100644 index 0000000..a83923c --- /dev/null +++ b/Backend.Api/Data/DataContext.cs @@ -0,0 +1,14 @@ +using Microsoft.EntityFrameworkCore; + +namespace Backend.Api.Data +{ + public class DataContext : DbContext + { + public DataContext(DbContextOptions options) : base(options) + { + + } + public DbSet Users { get; set; } + public DbSet Vehicles { get; set; } + } +} diff --git a/Backend.Api/Data/Services/UserService.cs b/Backend.Api/Data/Services/UserService.cs new file mode 100644 index 0000000..e780027 --- /dev/null +++ b/Backend.Api/Data/Services/UserService.cs @@ -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 GetVehicles(long id) + { + var userId = _context.Users.FirstOrDefault(x => x.Id == id).Id; + var vehicles = _context.Vehicles.Where(x => x.OwnerId == userId).ToList(); + return vehicles; + } + } +} diff --git a/Backend.Api/Data/Services/VehicleService.cs b/Backend.Api/Data/Services/VehicleService.cs new file mode 100644 index 0000000..1359761 --- /dev/null +++ b/Backend.Api/Data/Services/VehicleService.cs @@ -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); + } + + } +} diff --git a/Backend.Api/Data/User.cs b/Backend.Api/Data/User.cs new file mode 100644 index 0000000..37ca7b4 --- /dev/null +++ b/Backend.Api/Data/User.cs @@ -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"; + } +} diff --git a/Backend.Api/Data/Vehicle.cs b/Backend.Api/Data/Vehicle.cs new file mode 100644 index 0000000..82aa8a3 --- /dev/null +++ b/Backend.Api/Data/Vehicle.cs @@ -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"; + } +} diff --git a/Backend.Api/Dockerfile b/Backend.Api/Dockerfile new file mode 100644 index 0000000..36c00c2 --- /dev/null +++ b/Backend.Api/Dockerfile @@ -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"] \ No newline at end of file diff --git a/Backend.Api/Program.cs b/Backend.Api/Program.cs index 48863a6..b29039e 100644 --- a/Backend.Api/Program.cs +++ b/Backend.Api/Program.cs @@ -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); // Add services to the container. @@ -5,8 +10,11 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(); +builder.Services.AddDbContext(options => options.UseNpgsql("")); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/Backend.Api/Properties/launchSettings.json b/Backend.Api/Properties/launchSettings.json index 3a67877..26f470c 100644 --- a/Backend.Api/Properties/launchSettings.json +++ b/Backend.Api/Properties/launchSettings.json @@ -1,23 +1,14 @@ -{ - "$schema": "https://json.schemastore.org/launchsettings.json", - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:52732", - "sslPort": 44361 - } - }, +{ "profiles": { "Backend.Api": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, "launchUrl": "swagger", - "applicationUrl": "https://localhost:7070;http://localhost:5297", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "dotnetRunMessages": true, + "applicationUrl": "https://localhost:7070;http://localhost:5297" }, "IIS Express": { "commandName": "IISExpress", @@ -26,6 +17,22 @@ "environmentVariables": { "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 } } -} +} \ No newline at end of file diff --git a/Backend.Api/WeatherForecast.cs b/Backend.Api/WeatherForecast.cs deleted file mode 100644 index 2b0792b..0000000 --- a/Backend.Api/WeatherForecast.cs +++ /dev/null @@ -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; } -}