From 03302065abf2dc911c9533c207b9c2f592831ec1 Mon Sep 17 00:00:00 2001 From: mfnefd Date: Tue, 12 Nov 2024 19:18:37 +0400 Subject: [PATCH 1/4] =?UTF-8?q?fix:=20mac=20=D1=82=D0=B5=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D1=8C=20=D0=BD=D0=B5=20=D0=B8=D0=BF=D0=BE=D0=BB=D1=8C=D0=B7?= =?UTF-8?q?=D1=83=D0=B5=D1=82=D1=81=D1=8F.=20ip=20=D0=BD=D0=B0=D1=88=D0=B5?= =?UTF-8?q?=20=D0=B2=D1=81=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cloud/Controllers/FarmController.cs | 204 ++++++++++++++-------------- Cloud/Models/Farm.cs | 19 ++- Cloud/Requests/FarmRequest.cs | 12 +- Cloud/Validation/FarmValidator.cs | 24 ++-- 4 files changed, 129 insertions(+), 130 deletions(-) diff --git a/Cloud/Controllers/FarmController.cs b/Cloud/Controllers/FarmController.cs index a1e7b73..b5cf078 100644 --- a/Cloud/Controllers/FarmController.cs +++ b/Cloud/Controllers/FarmController.cs @@ -6,123 +6,123 @@ using Microsoft.EntityFrameworkCore; namespace Cloud.Controllers { - [Authorize] - [ApiController] - [Route("api/user")] - public class FarmController : ControllerBase - { - private IConfiguration _config; - private ApplicationContext _context; + [Authorize] + [ApiController] + [Route("api/user")] + public class FarmController : ControllerBase + { + private IConfiguration _config; + private ApplicationContext _context; - public FarmController(IConfiguration config, ApplicationContext context) - { - _config = config; - _context = context; - } + public FarmController(IConfiguration config, ApplicationContext context) + { + _config = config; + _context = context; + } - [HttpGet("{userId}/farm")] - public async Task>> Index (int userId) - { - try - { - List farms = await - _context.Farms.Where(x => x.UserId == userId).AsNoTracking().ToListAsync(); - if (!farms.Any()) - return NotFound("Farms is not found"); + [HttpGet("{userId}/farm")] + public async Task>> Index(int userId) + { + try + { + List farms = await + _context.Farms.Where(x => x.UserId == userId).AsNoTracking().ToListAsync(); + if (!farms.Any()) + return NotFound("Farms is not found"); - return Ok(farms); - } - catch (Exception ex) - { - return BadRequest(ex.Message); - } - } + return Ok(farms); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } - [HttpGet("{userId}/farm/{farmId}")] - public async Task> Show(int userId, int farmId) - { - try - { - Farm? farm = await - _context.Farms.FirstOrDefaultAsync(x => x.UserId == userId && x.Id == farmId); + [HttpGet("{userId}/farm/{farmId}")] + public async Task> Show(int userId, int farmId) + { + try + { + Farm? farm = await + _context.Farms.FirstOrDefaultAsync(x => x.UserId == userId && x.Id == farmId); - if (farm == null) - return NotFound("Farm is not found"); + if (farm == null) + return NotFound("Farm is not found"); - return Ok(farm); - } - catch (Exception ex) - { - return BadRequest(ex.Message); - } - } + return Ok(farm); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } - [HttpPost("{userId}/farm")] - public async Task> Create([FromBody] FarmRequest farmRequest, int userId) - { - try - { - var farm = new Farm { - Name = farmRequest.Name, - UserId = userId, - RaspberryMacAddr = farmRequest.RaspberryMacAddr, - }; + [HttpPost("{userId}/farm")] + public async Task> Create([FromBody] FarmRequest farmRequest, int userId) + { + try + { + var farm = new Farm + { + Name = farmRequest.Name, + UserId = userId, + RaspberryIP = farmRequest.RaspberryIP, + }; - Farm? farmCreated = _context.Farms.Add(farm).Entity; - await _context.SaveChangesAsync(); + Farm? farmCreated = _context.Farms.Add(farm).Entity; + await _context.SaveChangesAsync(); - return Ok(farmCreated); - } - catch (Exception ex) - { - return BadRequest(ex.Message); - } - } + return Ok(farmCreated); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } - [HttpPut("{userId}/farm/{farmId}")] - public async Task> Update([FromBody] FarmRequest farmRequest, int userId, int farmId) - { - try - { - Farm? farm = await _context.Farms.FirstOrDefaultAsync(x => x.Id == farmId && x.UserId == userId); + [HttpPut("{userId}/farm/{farmId}")] + public async Task> Update([FromBody] FarmRequest farmRequest, int userId, int farmId) + { + try + { + Farm? farm = await _context.Farms.FirstOrDefaultAsync(x => x.Id == farmId && x.UserId == userId); - if (farm == null) - return NotFound("Farm is not found"); + if (farm == null) + return NotFound("Farm is not found"); - farm.Name = farmRequest.Name; - farm.RaspberryMacAddr = farmRequest.RaspberryMacAddr; + farm.Name = farmRequest.Name; + farm.RaspberryIP = farmRequest.RaspberryIP; - _context.Farms.Update(farm); - await _context.SaveChangesAsync(); + _context.Farms.Update(farm); + await _context.SaveChangesAsync(); - return Ok(farm); - } - catch (Exception ex) - { - return BadRequest(ex.Message); - } - } + return Ok(farm); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } - [HttpDelete("{userId}/farm/{farmId}")] - public async Task Delete(int userId, int farmId) - { - try - { - Farm? farm = await _context.Farms.FirstOrDefaultAsync(x => x.Id == farmId && x.UserId == userId); + [HttpDelete("{userId}/farm/{farmId}")] + public async Task Delete(int userId, int farmId) + { + try + { + Farm? farm = await _context.Farms.FirstOrDefaultAsync(x => x.Id == farmId && x.UserId == userId); - if (farm == null) - return NotFound("Farm is not found"); + if (farm == null) + return NotFound("Farm is not found"); - _context.Farms.Remove(farm); - await _context.SaveChangesAsync(); + _context.Farms.Remove(farm); + await _context.SaveChangesAsync(); - return Ok("Farm deleted successfully"); - } - catch (Exception ex) - { - return BadRequest(ex.Message); - } - } - - } -} + return Ok("Farm deleted successfully"); + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + } + } +} \ No newline at end of file diff --git a/Cloud/Models/Farm.cs b/Cloud/Models/Farm.cs index a17d3bc..5cc1ac8 100644 --- a/Cloud/Models/Farm.cs +++ b/Cloud/Models/Farm.cs @@ -1,12 +1,11 @@ namespace Cloud.Models { - public class Farm - { - public int Id { get; set; } - public string Name { get; set; } - public int UserId { get; set; } - public User? User { get; set; } - public string RaspberryMacAddr { get; set; } - - } -} + public class Farm + { + public int Id { get; set; } + public string Name { get; set; } + public int UserId { get; set; } + public User? User { get; set; } + public string RaspberryIP { get; set; } + } +} \ No newline at end of file diff --git a/Cloud/Requests/FarmRequest.cs b/Cloud/Requests/FarmRequest.cs index c032dfa..3371488 100644 --- a/Cloud/Requests/FarmRequest.cs +++ b/Cloud/Requests/FarmRequest.cs @@ -1,8 +1,8 @@ namespace Cloud.Requests { - public class FarmRequest - { - public string Name { get; set; } - public string RaspberryMacAddr { get; set; } - } -} + public class FarmRequest + { + public string Name { get; set; } + public string RaspberryIP { get; set; } + } +} \ No newline at end of file diff --git a/Cloud/Validation/FarmValidator.cs b/Cloud/Validation/FarmValidator.cs index 4c262fb..c4df29e 100644 --- a/Cloud/Validation/FarmValidator.cs +++ b/Cloud/Validation/FarmValidator.cs @@ -3,16 +3,16 @@ using FluentValidation; namespace Cloud.Validation { - public class FarmValidator : AbstractValidator - { - public FarmValidator() - { - RuleFor(request => request.RaspberryMacAddr) - .NotEmpty().WithMessage("MAC address can't be empty") - .Matches("^([0-9A-Fa-f]{2}[:-]?){5}([0-9A-Fa-f]{2})$").WithMessage("MAC address is not valid"); + public class FarmValidator : AbstractValidator + { + public FarmValidator() + { + RuleFor(request => request.RaspberryIP) + .NotEmpty().WithMessage("IP address can't be empty") + .Matches(@"^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$").WithMessage("IP address is not valid"); - RuleFor(request => request.Name) - .NotEmpty().WithMessage("Name can't be empty"); - } - } -} + RuleFor(request => request.Name) + .NotEmpty().WithMessage("Name can't be empty"); + } + } +} \ No newline at end of file From 488c91d2b173b3b509c45a98620534cc8c18f8fd Mon Sep 17 00:00:00 2001 From: mfnefd Date: Tue, 12 Nov 2024 19:48:58 +0400 Subject: [PATCH 2/4] =?UTF-8?q?del:=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD?= =?UTF-8?q?=D1=8B=20=D0=BD=D0=B5=D0=BD=D1=83=D0=B6=D0=BD=D1=8B=D0=B5=20?= =?UTF-8?q?=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B6=D0=B5=D1=81=D1=82=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/WeatherForecastController.cs | 32 ------------------- Cloud/WeatherForecast.cs | 12 ------- 2 files changed, 44 deletions(-) delete mode 100644 Cloud/Controllers/WeatherForecastController.cs delete mode 100644 Cloud/WeatherForecast.cs diff --git a/Cloud/Controllers/WeatherForecastController.cs b/Cloud/Controllers/WeatherForecastController.cs deleted file mode 100644 index 5f76bcd..0000000 --- a/Cloud/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,32 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace Cloud.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/Cloud/WeatherForecast.cs b/Cloud/WeatherForecast.cs deleted file mode 100644 index d787653..0000000 --- a/Cloud/WeatherForecast.cs +++ /dev/null @@ -1,12 +0,0 @@ -namespace Cloud; - -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; } -} From 5adec563ac3f4553b779d89f3ffa2a03ff7d844d Mon Sep 17 00:00:00 2001 From: mfnefd Date: Tue, 12 Nov 2024 19:57:44 +0400 Subject: [PATCH 3/4] =?UTF-8?q?add:=20dockerfile=20=D0=B8=20=D0=B8=D0=B7?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD=20=D1=87=D1=83=D1=82=D0=BA=D0=B0?= =?UTF-8?q?=20docker-compose?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 25 +++++++++++++++++++++++++ .vscode/launch.json | 35 +++++++++++++++++++++++++++++++++++ .vscode/tasks.json | 41 +++++++++++++++++++++++++++++++++++++++++ Cloud/Dockerfile | 28 ++++++++++++++++++++++++++++ docker-compose.yml | 6 ++++++ 5 files changed, 135 insertions(+) create mode 100644 .dockerignore create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 Cloud/Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3dbbcf3 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,25 @@ +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/bin +**/charts +**/docker-compose* +**/compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..e8a7501 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,35 @@ +{ + "version": "0.2.0", + "configurations": [ + { + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/dotnet/vscode-csharp/blob/main/debugger-launchjson.md. + "name": ".NET Core Launch (web)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/Cloud/bin/Debug/net6.0/Cloud.dll", + "args": [], + "cwd": "${workspaceFolder}/Cloud", + "stopAtEntry": false, + // Enable launching a web browser when ASP.NET Core starts. For more information: https://aka.ms/VSCode-CS-LaunchJson-WebBrowser + "serverReadyAction": { + "action": "openExternally", + "pattern": "\\bNow listening on:\\s+(https?://\\S+)" + }, + "env": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "sourceFileMap": { + "/Views": "${workspaceFolder}/Views" + } + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..d410234 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,41 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/Cloud.sln", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary;ForceNoAlign" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "publish", + "command": "dotnet", + "type": "process", + "args": [ + "publish", + "${workspaceFolder}/Cloud.sln", + "/property:GenerateFullPaths=true", + "/consoleloggerparameters:NoSummary;ForceNoAlign" + ], + "problemMatcher": "$msCompile" + }, + { + "label": "watch", + "command": "dotnet", + "type": "process", + "args": [ + "watch", + "run", + "--project", + "${workspaceFolder}/Cloud.sln" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/Cloud/Dockerfile b/Cloud/Dockerfile new file mode 100644 index 0000000..6de9e42 --- /dev/null +++ b/Cloud/Dockerfile @@ -0,0 +1,28 @@ +FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base +WORKDIR /app +EXPOSE 5124 + +ENV ASPNETCORE_URLS=http://+:5124 + +# Creates a non-root user with an explicit UID and adds permission to access the /app folder +# For more info, please refer to https://aka.ms/vscode-docker-dotnet-configure-containers +RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app +USER appuser + +FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:6.0 AS build +ARG configuration=Release +WORKDIR /src +COPY ["Cloud/Cloud.csproj", "Cloud/"] +RUN dotnet restore "Cloud/Cloud.csproj" +COPY . . +WORKDIR "/src/Cloud" +RUN dotnet build "Cloud.csproj" -c $configuration -o /app/build + +FROM build AS publish +ARG configuration=Release +RUN dotnet publish "Cloud.csproj" -c $configuration -o /app/publish /p:UseAppHost=false + +FROM base AS final +WORKDIR /app +COPY --from=publish /app/publish . +ENTRYPOINT ["dotnet", "Cloud.dll"] diff --git a/docker-compose.yml b/docker-compose.yml index 6915ce1..532e4cb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,10 @@ services: + cloud: + build: ./Cloud/ + ports: + - "5124:5124" + depends_on: + - postgres postgres: image: postgres:14 container_name: cucumber_database From 3ce4d6baf2845981909203480e72fdcd10fdab87 Mon Sep 17 00:00:00 2001 From: mfnefd Date: Wed, 13 Nov 2024 01:49:13 +0400 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20dockerfile=20=D0=B8=20docker-compose?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cloud/Dockerfile | 13 +++++++------ docker-compose.yml | 17 ++++++++--------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Cloud/Dockerfile b/Cloud/Dockerfile index 6de9e42..084ab62 100644 --- a/Cloud/Dockerfile +++ b/Cloud/Dockerfile @@ -9,20 +9,21 @@ ENV ASPNETCORE_URLS=http://+:5124 RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app USER appuser -FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:6.0 AS build +FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build ARG configuration=Release WORKDIR /src -COPY ["Cloud/Cloud.csproj", "Cloud/"] -RUN dotnet restore "Cloud/Cloud.csproj" +COPY ["Cloud.csproj", "."] +RUN dotnet restore "./Cloud.csproj" COPY . . -WORKDIR "/src/Cloud" -RUN dotnet build "Cloud.csproj" -c $configuration -o /app/build +WORKDIR "/src/." +RUN dotnet build "./Cloud.csproj" -c $configuration -o /app/build FROM build AS publish ARG configuration=Release -RUN dotnet publish "Cloud.csproj" -c $configuration -o /app/publish /p:UseAppHost=false +RUN dotnet publish "./Cloud.csproj" -c $configuration -o /app/publish /p:UseAppHost=false FROM base AS final WORKDIR /app COPY --from=publish /app/publish . ENTRYPOINT ["dotnet", "Cloud.dll"] + diff --git a/docker-compose.yml b/docker-compose.yml index 532e4cb..8503dba 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,22 +1,21 @@ services: cloud: build: ./Cloud/ - ports: + ports: - "5124:5124" depends_on: - postgres postgres: - image: postgres:14 + image: postgres:14 container_name: cucumber_database environment: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: 12345 - POSTGRES_DB: main_database + POSTGRES_USER: postgres + POSTGRES_PASSWORD: 12345 + POSTGRES_DB: main_database ports: - - "5438:5432" + - "5438:5432" volumes: - - postgres_data:/var/lib/postgresql/data - + - postgres_data:/var/lib/postgresql/data volumes: postgres_data: - driver: local \ No newline at end of file + driver: local \ No newline at end of file