Compare commits
18 Commits
main
...
greenhouse
Author | SHA1 | Date | |
---|---|---|---|
5687949f96 | |||
2a9508f737 | |||
1e2bd05667 | |||
fbfde769b1 | |||
49c7d14925 | |||
3ce4d6baf2 | |||
5adec563ac | |||
488c91d2b1 | |||
03302065ab | |||
80b002f12a | |||
|
a4dae6211f | ||
|
5d7beec9c1 | ||
78aea1397e | |||
c4899d4a11 | |||
33c3a074da | |||
|
0bf5823652 | ||
|
0c94c926a5 | ||
|
c58d07731c |
25
.dockerignore
Normal file
25
.dockerignore
Normal file
@ -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
|
35
.vscode/launch.json
vendored
Normal file
35
.vscode/launch.json
vendored
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"dotnet.defaultSolution": "Cloud.sln"
|
||||||
|
}
|
41
.vscode/tasks.json
vendored
Normal file
41
.vscode/tasks.json
vendored
Normal file
@ -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"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
22
Cloud.sln
Normal file
22
Cloud.sln
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio Version 17
|
||||||
|
VisualStudioVersion = 17.0.31903.59
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Cloud", "Cloud\Cloud.csproj", "{D279AAA9-D4F8-4C7B-B34D-44A7429C87AA}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{D279AAA9-D4F8-4C7B-B34D-44A7429C87AA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{D279AAA9-D4F8-4C7B-B34D-44A7429C87AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{D279AAA9-D4F8-4C7B-B34D-44A7429C87AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{D279AAA9-D4F8-4C7B-B34D-44A7429C87AA}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
31
Cloud/ApplicationContext.cs
Normal file
31
Cloud/ApplicationContext.cs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
using Cloud.Models;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Cloud;
|
||||||
|
public class ApplicationContext : DbContext
|
||||||
|
{
|
||||||
|
public DbSet<User> Users { get; set; } = null!;
|
||||||
|
public DbSet<Farm> Farms { get; set; } = null!;
|
||||||
|
public DbSet<Greenhouse> Greenhouses { get; set; } = null!;
|
||||||
|
|
||||||
|
public ApplicationContext(DbContextOptions<ApplicationContext> options)
|
||||||
|
: base(options)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public ApplicationContext()
|
||||||
|
: base(new DbContextOptionsBuilder<ApplicationContext>()
|
||||||
|
.UseNpgsql("Host=localhost;Port=5438;Database=main_database;Username=postgres;Password=12345")
|
||||||
|
.Options)
|
||||||
|
{
|
||||||
|
Database.EnsureCreated();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||||
|
{
|
||||||
|
if (!optionsBuilder.IsConfigured)
|
||||||
|
{
|
||||||
|
optionsBuilder.UseNpgsql("Host=localhost;Port=5438;Database=main_database;Username=postgres;Password=12345");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
23
Cloud/Cloud.csproj
Normal file
23
Cloud/Cloud.csproj
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net6.0</TargetFramework>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
|
||||||
|
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.4" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.14" />
|
||||||
|
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.6">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.7" />
|
||||||
|
<PackageReference Include="StackExchange.Redis" Version="2.8.16" />
|
||||||
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
|
||||||
|
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.35.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
104
Cloud/Controllers/AuthController.cs
Normal file
104
Cloud/Controllers/AuthController.cs
Normal file
@ -0,0 +1,104 @@
|
|||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
using Cloud.Models;
|
||||||
|
using Cloud.Requests;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
using System.IdentityModel.Tokens.Jwt;
|
||||||
|
using System.Text;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using System.Security.Claims;
|
||||||
|
|
||||||
|
namespace Cloud.Controllers;
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/[controller]")]
|
||||||
|
public class AuthController : ControllerBase
|
||||||
|
{
|
||||||
|
private PasswordHasher<User> _passwordHasher;
|
||||||
|
private IConfiguration _config;
|
||||||
|
private ApplicationContext _context;
|
||||||
|
|
||||||
|
public AuthController(IConfiguration config, ApplicationContext context)
|
||||||
|
{
|
||||||
|
_passwordHasher = new PasswordHasher<User>();
|
||||||
|
_config = config;
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("register")]
|
||||||
|
public async Task<IActionResult> Register([FromBody] RegisterRequest request)
|
||||||
|
{
|
||||||
|
var existUser = await _context.Users.SingleOrDefaultAsync(u => u.Email == request.Email);
|
||||||
|
|
||||||
|
if (existUser != null) {
|
||||||
|
return BadRequest("Пользователь с такой эл. почтой уже существует");
|
||||||
|
}
|
||||||
|
|
||||||
|
var user = new User
|
||||||
|
{
|
||||||
|
Name = request.Name,
|
||||||
|
Email = request.Email,
|
||||||
|
Password = _passwordHasher.HashPassword(null, request.Password)
|
||||||
|
};
|
||||||
|
|
||||||
|
_context.Users.Add(user);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return Ok("Пользователь успешно зарегистрирован");
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("login")]
|
||||||
|
public async Task<IActionResult> Login([FromBody] LoginRequest request)
|
||||||
|
{
|
||||||
|
var user = await _context.Users.SingleOrDefaultAsync(u => u.Email == request.Email);
|
||||||
|
|
||||||
|
if (user == null) {
|
||||||
|
return Unauthorized("Пользователя с такой эл. почтой не существует");
|
||||||
|
}
|
||||||
|
|
||||||
|
var verificationResult = _passwordHasher.VerifyHashedPassword(null, user.Password, request.Password);
|
||||||
|
|
||||||
|
if (verificationResult == PasswordVerificationResult.Failed) {
|
||||||
|
return Unauthorized("Неверный пароль");
|
||||||
|
}
|
||||||
|
|
||||||
|
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]));
|
||||||
|
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
|
||||||
|
|
||||||
|
var claims = new[]
|
||||||
|
{
|
||||||
|
new Claim(ClaimTypes.Name, user.Email),
|
||||||
|
};
|
||||||
|
|
||||||
|
var Sectoken = new JwtSecurityToken(_config["Jwt:Issuer"],
|
||||||
|
_config["Jwt:Issuer"],
|
||||||
|
claims: claims,
|
||||||
|
expires: DateTime.Now.AddMinutes(120),
|
||||||
|
signingCredentials: credentials);
|
||||||
|
|
||||||
|
var token = new JwtSecurityTokenHandler().WriteToken(Sectoken);
|
||||||
|
|
||||||
|
return Ok(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
[HttpGet("user")]
|
||||||
|
public async Task<IActionResult> GetAuthUser()
|
||||||
|
{
|
||||||
|
var userEmail = User.Identity.Name;
|
||||||
|
|
||||||
|
var user = await _context.Users.SingleOrDefaultAsync(u => u.Email == userEmail);
|
||||||
|
|
||||||
|
if (user == null) {
|
||||||
|
return NotFound("Пользователь не найден");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(new
|
||||||
|
{
|
||||||
|
user.Id,
|
||||||
|
user.Name,
|
||||||
|
user.Email
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
128
Cloud/Controllers/FarmController.cs
Normal file
128
Cloud/Controllers/FarmController.cs
Normal file
@ -0,0 +1,128 @@
|
|||||||
|
using Cloud.Models;
|
||||||
|
using Cloud.Requests;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Cloud.Controllers
|
||||||
|
{
|
||||||
|
[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;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{userId}/farm")]
|
||||||
|
public async Task<ActionResult<List<Farm>>> Index(int userId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
List<Farm> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet("{userId}/farm/{farmId}")]
|
||||||
|
public async Task<ActionResult<Farm>> 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");
|
||||||
|
|
||||||
|
return Ok(farm);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return BadRequest(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("{userId}/farm")]
|
||||||
|
public async Task<ActionResult<Farm>> 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();
|
||||||
|
|
||||||
|
return Ok(farmCreated);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return BadRequest(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPut("{userId}/farm/{farmId}")]
|
||||||
|
public async Task<ActionResult<Farm>> 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");
|
||||||
|
|
||||||
|
farm.Name = farmRequest.Name;
|
||||||
|
farm.RaspberryIP = farmRequest.RaspberryIP;
|
||||||
|
|
||||||
|
_context.Farms.Update(farm);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return Ok(farm);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return BadRequest(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpDelete("{userId}/farm/{farmId}")]
|
||||||
|
public async Task<ActionResult> 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");
|
||||||
|
|
||||||
|
_context.Farms.Remove(farm);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return Ok("Farm deleted successfully");
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return BadRequest(ex.Message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
79
Cloud/Controllers/GreengouseController.cs
Normal file
79
Cloud/Controllers/GreengouseController.cs
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
using Cloud.Models;
|
||||||
|
using Cloud.Requests;
|
||||||
|
using Cloud.Services.Broker;
|
||||||
|
using Cloud.Services.Broker.Support;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
|
||||||
|
namespace Cloud.Controllers
|
||||||
|
{
|
||||||
|
[ApiController]
|
||||||
|
[Route("api/user/{userId}/farm/{farmId}/greenhouse")]
|
||||||
|
public class GreenhouseController : ControllerBase
|
||||||
|
{
|
||||||
|
private readonly IBrokerService _brokerService;
|
||||||
|
private readonly ApplicationContext _context;
|
||||||
|
private readonly IConfiguration _config;
|
||||||
|
public GreenhouseController(IConfiguration config, ApplicationContext context,
|
||||||
|
IBrokerService brokerService)
|
||||||
|
{
|
||||||
|
_brokerService = brokerService;
|
||||||
|
_context = context;
|
||||||
|
_config = config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Возвращает текущую информацию о всех теплицах пользователя
|
||||||
|
*/
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<ActionResult<List<GreenhouseInfo>>> GetAll(int userId, int farmId)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Возвращает текущую информацию о конкретной теплице
|
||||||
|
*/
|
||||||
|
[HttpGet("{greenhouseId}")]
|
||||||
|
public async Task<ActionResult<GreenhouseInfo>> Get(int userId, int farmId, int greenhouseId)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Возвращает сохраненные данные для автоматизации теплицы
|
||||||
|
*/
|
||||||
|
[HttpGet("{greenhouseId}/settings")]
|
||||||
|
public async Task<ActionResult<Greenhouse>> GetGreenhouse(int userId, int farmId, int greenhouseId)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Сохраняет в базе данных API данные для автоматизации теплицы
|
||||||
|
*/
|
||||||
|
[HttpPost]
|
||||||
|
public async Task<ActionResult<Greenhouse>> SaveToDatabase(int userId, int farmId, GreenhouseRequest greenhouse)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Обновляет в базе данных API данные для автоматизации теплицы
|
||||||
|
*/
|
||||||
|
[HttpPut("{greenhouseId}/settings")]
|
||||||
|
public async Task<ActionResult<Greenhouse>> Update(int userId, int farmId, int greenhouseId, GreenhouseRequest greenhouse)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Удаляет из базы данных API запись настроек автоматизации теплицы
|
||||||
|
*/
|
||||||
|
[HttpDelete("{greenhouseId}")]
|
||||||
|
public async Task<ActionResult> Delete(int userId, int farmId, int greenhouseId)
|
||||||
|
{
|
||||||
|
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
30
Cloud/Dockerfile
Normal file
30
Cloud/Dockerfile
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
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 mcr.microsoft.com/dotnet/sdk:6.0 AS build
|
||||||
|
ARG configuration=Release
|
||||||
|
WORKDIR /src
|
||||||
|
COPY ["Cloud.csproj", "."]
|
||||||
|
RUN dotnet restore "./Cloud.csproj"
|
||||||
|
COPY . .
|
||||||
|
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
|
||||||
|
|
||||||
|
FROM base AS final
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=publish /app/publish .
|
||||||
|
# TODO: Добавить инициализацию базы данных с помощью миграции
|
||||||
|
ENTRYPOINT ["dotnet", "Cloud.dll"]
|
||||||
|
|
53
Cloud/Migrations/20241027220558_CreateUsersTable.Designer.cs
generated
Normal file
53
Cloud/Migrations/20241027220558_CreateUsersTable.Designer.cs
generated
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using Cloud;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Cloud.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(ApplicationContext))]
|
||||||
|
[Migration("20241027220558_CreateUsersTable")]
|
||||||
|
partial class CreateUsersTable
|
||||||
|
{
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "6.0.14")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||||
|
|
||||||
|
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("Cloud.Models.User", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("integer");
|
||||||
|
|
||||||
|
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<string>("Email")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.Property<string>("Password")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Users");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
34
Cloud/Migrations/20241027220558_CreateUsersTable.cs
Normal file
34
Cloud/Migrations/20241027220558_CreateUsersTable.cs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Cloud.Migrations
|
||||||
|
{
|
||||||
|
public partial class CreateUsersTable : Migration
|
||||||
|
{
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "Users",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<int>(type: "integer", nullable: false)
|
||||||
|
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||||
|
Name = table.Column<string>(type: "text", nullable: false),
|
||||||
|
Email = table.Column<string>(type: "text", nullable: false),
|
||||||
|
Password = table.Column<string>(type: "text", nullable: false)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_Users", x => x.Id);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "Users");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
95
Cloud/Migrations/20241028192806_CreateFarmsTable.Designer.cs
generated
Normal file
95
Cloud/Migrations/20241028192806_CreateFarmsTable.Designer.cs
generated
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using Cloud;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Cloud.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(ApplicationContext))]
|
||||||
|
[Migration("20241028192806_CreateFarmsTable")]
|
||||||
|
partial class CreateFarmsTable
|
||||||
|
{
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "6.0.14")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||||
|
|
||||||
|
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("Cloud.Models.Farm", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("integer");
|
||||||
|
|
||||||
|
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.Property<string>("RaspberryMacAddr")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.Property<int>("UserId")
|
||||||
|
.HasColumnType("integer");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("UserId");
|
||||||
|
|
||||||
|
b.ToTable("Farms");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Cloud.Models.User", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("integer");
|
||||||
|
|
||||||
|
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<string>("Email")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.Property<string>("Password")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Users");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Cloud.Models.Farm", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("Cloud.Models.User", "User")
|
||||||
|
.WithMany("Farms")
|
||||||
|
.HasForeignKey("UserId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("User");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Cloud.Models.User", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("Farms");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
45
Cloud/Migrations/20241028192806_CreateFarmsTable.cs
Normal file
45
Cloud/Migrations/20241028192806_CreateFarmsTable.cs
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Cloud.Migrations
|
||||||
|
{
|
||||||
|
public partial class CreateFarmsTable : Migration
|
||||||
|
{
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.CreateTable(
|
||||||
|
name: "Farms",
|
||||||
|
columns: table => new
|
||||||
|
{
|
||||||
|
Id = table.Column<int>(type: "integer", nullable: false)
|
||||||
|
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
|
||||||
|
Name = table.Column<string>(type: "text", nullable: false),
|
||||||
|
UserId = table.Column<int>(type: "integer", nullable: false),
|
||||||
|
RaspberryMacAddr = table.Column<string>(type: "text", nullable: false)
|
||||||
|
},
|
||||||
|
constraints: table =>
|
||||||
|
{
|
||||||
|
table.PrimaryKey("PK_Farms", x => x.Id);
|
||||||
|
table.ForeignKey(
|
||||||
|
name: "FK_Farms_Users_UserId",
|
||||||
|
column: x => x.UserId,
|
||||||
|
principalTable: "Users",
|
||||||
|
principalColumn: "Id",
|
||||||
|
onDelete: ReferentialAction.Cascade);
|
||||||
|
});
|
||||||
|
|
||||||
|
migrationBuilder.CreateIndex(
|
||||||
|
name: "IX_Farms_UserId",
|
||||||
|
table: "Farms",
|
||||||
|
column: "UserId");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropTable(
|
||||||
|
name: "Farms");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
95
Cloud/Migrations/20241030111034_front.Designer.cs
generated
Normal file
95
Cloud/Migrations/20241030111034_front.Designer.cs
generated
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using Cloud;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Cloud.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(ApplicationContext))]
|
||||||
|
[Migration("20241030111034_front")]
|
||||||
|
partial class front
|
||||||
|
{
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "6.0.14")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||||
|
|
||||||
|
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("Cloud.Models.Farm", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("integer");
|
||||||
|
|
||||||
|
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.Property<string>("RaspberryMacAddr")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.Property<int>("UserId")
|
||||||
|
.HasColumnType("integer");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("UserId");
|
||||||
|
|
||||||
|
b.ToTable("Farms");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Cloud.Models.User", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("integer");
|
||||||
|
|
||||||
|
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<string>("Email")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.Property<string>("Password")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Users");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Cloud.Models.Farm", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("Cloud.Models.User", "User")
|
||||||
|
.WithMany("Farms")
|
||||||
|
.HasForeignKey("UserId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("User");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Cloud.Models.User", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("Farms");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
19
Cloud/Migrations/20241030111034_front.cs
Normal file
19
Cloud/Migrations/20241030111034_front.cs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Cloud.Migrations
|
||||||
|
{
|
||||||
|
public partial class front : Migration
|
||||||
|
{
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
93
Cloud/Migrations/ApplicationContextModelSnapshot.cs
Normal file
93
Cloud/Migrations/ApplicationContextModelSnapshot.cs
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using Cloud;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Cloud.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(ApplicationContext))]
|
||||||
|
partial class ApplicationContextModelSnapshot : ModelSnapshot
|
||||||
|
{
|
||||||
|
protected override void BuildModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "6.0.14")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 63);
|
||||||
|
|
||||||
|
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
|
||||||
|
|
||||||
|
modelBuilder.Entity("Cloud.Models.Farm", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("integer");
|
||||||
|
|
||||||
|
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.Property<string>("RaspberryMacAddr")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.Property<int>("UserId")
|
||||||
|
.HasColumnType("integer");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("UserId");
|
||||||
|
|
||||||
|
b.ToTable("Farms");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Cloud.Models.User", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("integer");
|
||||||
|
|
||||||
|
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
|
||||||
|
|
||||||
|
b.Property<string>("Email")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.Property<string>("Password")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("text");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Users");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Cloud.Models.Farm", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("Cloud.Models.User", "User")
|
||||||
|
.WithMany("Farms")
|
||||||
|
.HasForeignKey("UserId")
|
||||||
|
.OnDelete(DeleteBehavior.Cascade)
|
||||||
|
.IsRequired();
|
||||||
|
|
||||||
|
b.Navigation("User");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Cloud.Models.User", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("Farms");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
12
Cloud/Models/Farm.cs
Normal file
12
Cloud/Models/Farm.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
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 RaspberryIP { get; set; }
|
||||||
|
List<Greenhouse> Greenhouses { get; set; } = new();
|
||||||
|
}
|
||||||
|
}
|
14
Cloud/Models/Greenhouse.cs
Normal file
14
Cloud/Models/Greenhouse.cs
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
using Cloud.Models.Support;
|
||||||
|
|
||||||
|
namespace Cloud.Models
|
||||||
|
{
|
||||||
|
public class Greenhouse
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public int RecomendedTemperature { get; set; }
|
||||||
|
public WateringMode WateringMode { get; set; }
|
||||||
|
public HeatingMode HeatingMode { get; set; }
|
||||||
|
public int FarmId { get; set; }
|
||||||
|
public Farm? Farm { get; set; }
|
||||||
|
}
|
||||||
|
}
|
8
Cloud/Models/Support/HeatingMode.cs
Normal file
8
Cloud/Models/Support/HeatingMode.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace Cloud.Models.Support
|
||||||
|
{
|
||||||
|
public enum HeatingMode
|
||||||
|
{
|
||||||
|
Manual,
|
||||||
|
Auto
|
||||||
|
}
|
||||||
|
}
|
8
Cloud/Models/Support/WateringMode.cs
Normal file
8
Cloud/Models/Support/WateringMode.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace Cloud.Models.Support
|
||||||
|
{
|
||||||
|
public enum WateringMode
|
||||||
|
{
|
||||||
|
Manual,
|
||||||
|
Auto
|
||||||
|
}
|
||||||
|
}
|
13
Cloud/Models/User.cs
Normal file
13
Cloud/Models/User.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
namespace Cloud.Models;
|
||||||
|
public class User
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public string Email { get; set; }
|
||||||
|
|
||||||
|
public string Password { get; set; }
|
||||||
|
|
||||||
|
public List<Farm> Farms { get; set; } = new();
|
||||||
|
}
|
118
Cloud/Program.cs
Normal file
118
Cloud/Program.cs
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
using Cloud;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
using System.Text;
|
||||||
|
using FluentValidation;
|
||||||
|
using FluentValidation.AspNetCore;
|
||||||
|
using Cloud.Validation;
|
||||||
|
using StackExchange.Redis;
|
||||||
|
using Cloud.Services.Broker.Implement.Kafka;
|
||||||
|
using Cloud.Services.Broker;
|
||||||
|
|
||||||
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
// Add services to the container.
|
||||||
|
builder.Services.AddSingleton<IBrokerService, KafkaService>();
|
||||||
|
//Redis configuration
|
||||||
|
builder.Services.AddSingleton<IConnectionMultiplexer>(sp =>
|
||||||
|
{
|
||||||
|
var configuration = ConfigurationOptions.Parse("localhost:6379");
|
||||||
|
return ConnectionMultiplexer.Connect(configuration);
|
||||||
|
});
|
||||||
|
|
||||||
|
//Jwt configuration
|
||||||
|
var jwtIssuer = builder.Configuration.GetSection("Jwt:Issuer").Get<string>();
|
||||||
|
var jwtKey = builder.Configuration.GetSection("Jwt:Key").Get<string>();
|
||||||
|
|
||||||
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
||||||
|
.AddJwtBearer(options =>
|
||||||
|
{
|
||||||
|
options.TokenValidationParameters = new TokenValidationParameters
|
||||||
|
{
|
||||||
|
ValidateIssuer = true,
|
||||||
|
ValidateAudience = true,
|
||||||
|
ValidateLifetime = true,
|
||||||
|
ValidateIssuerSigningKey = true,
|
||||||
|
ValidIssuer = jwtIssuer,
|
||||||
|
ValidAudience = jwtIssuer,
|
||||||
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtKey))
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.Services.AddDbContext<ApplicationContext>(options =>
|
||||||
|
options.UseNpgsql("Host=localhost;Port=5438;Database=main_database;Username=postgres;Password=12345"));
|
||||||
|
|
||||||
|
// Настройка CORS
|
||||||
|
builder.Services.AddCors(options =>
|
||||||
|
{
|
||||||
|
options.AddPolicy("AllowFrontendLocalhost", builder =>
|
||||||
|
{
|
||||||
|
builder.WithOrigins("http://localhost:3000") // фронтенд
|
||||||
|
.AllowAnyHeader()
|
||||||
|
.AllowAnyMethod();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
builder.Services.AddControllers();
|
||||||
|
builder.Services.AddFluentValidationAutoValidation();
|
||||||
|
builder.Services.AddFluentValidationClientsideAdapters();
|
||||||
|
builder.Services.AddValidatorsFromAssemblyContaining<LoginValidator>();
|
||||||
|
builder.Services.AddValidatorsFromAssemblyContaining<RegisterValidator>();
|
||||||
|
builder.Services.AddValidatorsFromAssemblyContaining<FarmValidator>();
|
||||||
|
|
||||||
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||||
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
|
builder.Services.AddSwaggerGen(c =>
|
||||||
|
{
|
||||||
|
c.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Title = "Cloud API", Version = "v1" });
|
||||||
|
|
||||||
|
c.AddSecurityDefinition("Bearer", new Microsoft.OpenApi.Models.OpenApiSecurityScheme
|
||||||
|
{
|
||||||
|
Description = "Введите ваш Bearer токен",
|
||||||
|
Name = "Authorization",
|
||||||
|
In = Microsoft.OpenApi.Models.ParameterLocation.Header,
|
||||||
|
Type = Microsoft.OpenApi.Models.SecuritySchemeType.ApiKey
|
||||||
|
});
|
||||||
|
|
||||||
|
c.AddSecurityRequirement(new Microsoft.OpenApi.Models.OpenApiSecurityRequirement
|
||||||
|
{
|
||||||
|
{
|
||||||
|
new Microsoft.OpenApi.Models.OpenApiSecurityScheme
|
||||||
|
{
|
||||||
|
Reference = new Microsoft.OpenApi.Models.OpenApiReference
|
||||||
|
{
|
||||||
|
Type = Microsoft.OpenApi.Models.ReferenceType.SecurityScheme,
|
||||||
|
Id = "Bearer"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new string[] {}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
var app = builder.Build();
|
||||||
|
|
||||||
|
// Configure the HTTP request pipeline.
|
||||||
|
if (app.Environment.IsDevelopment())
|
||||||
|
{
|
||||||
|
app.UseSwagger();
|
||||||
|
app.UseSwaggerUI(c =>
|
||||||
|
{
|
||||||
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Cloud API V1");
|
||||||
|
c.RoutePrefix = string.Empty;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
app.UseHttpsRedirection();
|
||||||
|
|
||||||
|
// Включение CORS
|
||||||
|
app.UseCors("AllowFrontendLocalhost");
|
||||||
|
|
||||||
|
app.UseAuthentication();
|
||||||
|
|
||||||
|
app.UseAuthorization();
|
||||||
|
|
||||||
|
app.MapControllers();
|
||||||
|
|
||||||
|
app.Run();
|
31
Cloud/Properties/launchSettings.json
Normal file
31
Cloud/Properties/launchSettings.json
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://json.schemastore.org/launchsettings.json",
|
||||||
|
"iisSettings": {
|
||||||
|
"windowsAuthentication": false,
|
||||||
|
"anonymousAuthentication": true,
|
||||||
|
"iisExpress": {
|
||||||
|
"applicationUrl": "http://localhost:43967",
|
||||||
|
"sslPort": 44304
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"profiles": {
|
||||||
|
"Cloud": {
|
||||||
|
"commandName": "Project",
|
||||||
|
"dotnetRunMessages": true,
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"applicationUrl": "https://localhost:7113;http://localhost:5124",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"IIS Express": {
|
||||||
|
"commandName": "IISExpress",
|
||||||
|
"launchBrowser": true,
|
||||||
|
"launchUrl": "swagger",
|
||||||
|
"environmentVariables": {
|
||||||
|
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
8
Cloud/Requests/FarmRequest.cs
Normal file
8
Cloud/Requests/FarmRequest.cs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
namespace Cloud.Requests
|
||||||
|
{
|
||||||
|
public class FarmRequest
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
public string RaspberryIP { get; set; }
|
||||||
|
}
|
||||||
|
}
|
11
Cloud/Requests/GreenhouseRequest.cs
Normal file
11
Cloud/Requests/GreenhouseRequest.cs
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
using Cloud.Models.Support;
|
||||||
|
|
||||||
|
namespace Cloud.Requests
|
||||||
|
{
|
||||||
|
public class GreenhouseRequest
|
||||||
|
{
|
||||||
|
public int RecomendedTemperature { get; set; }
|
||||||
|
public WateringMode WateringMode { get; set; }
|
||||||
|
public HeatingMode HeatingMode { get; set; }
|
||||||
|
}
|
||||||
|
}
|
13
Cloud/Requests/LoginRequest.cs
Normal file
13
Cloud/Requests/LoginRequest.cs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Cloud.Requests;
|
||||||
|
|
||||||
|
public class LoginRequest
|
||||||
|
{
|
||||||
|
public string Email { get; set; }
|
||||||
|
|
||||||
|
public string Password { get; set; }
|
||||||
|
}
|
15
Cloud/Requests/RegisterRequest.cs
Normal file
15
Cloud/Requests/RegisterRequest.cs
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace Cloud.Requests;
|
||||||
|
|
||||||
|
public class RegisterRequest
|
||||||
|
{
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
public string Email { get; set; }
|
||||||
|
|
||||||
|
public string Password { get; set; }
|
||||||
|
}
|
7
Cloud/Services/Broker/IBrokerConsumer.cs
Normal file
7
Cloud/Services/Broker/IBrokerConsumer.cs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
namespace Cloud.Services.Broker
|
||||||
|
{
|
||||||
|
public interface IBrokerConsumer
|
||||||
|
{
|
||||||
|
// TODO: добавить методы для получения данных
|
||||||
|
}
|
||||||
|
}
|
9
Cloud/Services/Broker/IBrokerProdurcer.cs
Normal file
9
Cloud/Services/Broker/IBrokerProdurcer.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
using Cloud.Services.Broker.Support;
|
||||||
|
|
||||||
|
namespace Cloud.Services.Broker
|
||||||
|
{
|
||||||
|
public interface IBrokerProdurcer
|
||||||
|
{
|
||||||
|
Task ProduceAsync(string topic, Command command);
|
||||||
|
}
|
||||||
|
}
|
10
Cloud/Services/Broker/IBrokerService.cs
Normal file
10
Cloud/Services/Broker/IBrokerService.cs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
using Cloud.Services.Broker.Support;
|
||||||
|
|
||||||
|
namespace Cloud.Services.Broker
|
||||||
|
{
|
||||||
|
public interface IBrokerService
|
||||||
|
{
|
||||||
|
Task<CommandResult> Produce(Command command);
|
||||||
|
Task<T> Consume<T>(string topic);
|
||||||
|
}
|
||||||
|
}
|
17
Cloud/Services/Broker/Implement/Kafka/KafkaService.cs
Normal file
17
Cloud/Services/Broker/Implement/Kafka/KafkaService.cs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
using Cloud.Services.Broker.Support;
|
||||||
|
|
||||||
|
namespace Cloud.Services.Broker.Implement.Kafka
|
||||||
|
{
|
||||||
|
public class KafkaService : IBrokerService
|
||||||
|
{
|
||||||
|
public Task<T> Consume<T>(string topic)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<CommandResult> Produce(Command command)
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
9
Cloud/Services/Broker/Support/Command.cs
Normal file
9
Cloud/Services/Broker/Support/Command.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
namespace Cloud.Services.Broker.Support
|
||||||
|
{
|
||||||
|
public class Command
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public int GreenhouseId { get; set; }
|
||||||
|
public string CommandName { get; set; } = null!;
|
||||||
|
}
|
||||||
|
}
|
9
Cloud/Services/Broker/Support/CommandResult.cs
Normal file
9
Cloud/Services/Broker/Support/CommandResult.cs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
namespace Cloud.Services.Broker.Support
|
||||||
|
{
|
||||||
|
public class CommandResult
|
||||||
|
{
|
||||||
|
public int CommandId { get; set; }
|
||||||
|
public int GreenhouseId { get; set; }
|
||||||
|
public string ResultMessage { get; set; } = string.Empty;
|
||||||
|
}
|
||||||
|
}
|
12
Cloud/Services/Broker/Support/GreenhouseInfo.cs
Normal file
12
Cloud/Services/Broker/Support/GreenhouseInfo.cs
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
namespace Cloud.Services.Broker.Support
|
||||||
|
{
|
||||||
|
public class GreenhouseInfo
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public int PercentWater { get; set; }
|
||||||
|
public int SoilTemperature { get; set; }
|
||||||
|
public bool PumpStatus { get; set; }
|
||||||
|
public bool HeatingStatus { get; set; }
|
||||||
|
public bool AutoWateringStatus { get; set; }
|
||||||
|
}
|
||||||
|
}
|
18
Cloud/Validation/FarmValidator.cs
Normal file
18
Cloud/Validation/FarmValidator.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
using Cloud.Requests;
|
||||||
|
using FluentValidation;
|
||||||
|
|
||||||
|
namespace Cloud.Validation
|
||||||
|
{
|
||||||
|
public class FarmValidator : AbstractValidator<FarmRequest>
|
||||||
|
{
|
||||||
|
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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
18
Cloud/Validation/LoginValidator.cs
Normal file
18
Cloud/Validation/LoginValidator.cs
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
using Cloud.Requests;
|
||||||
|
using FluentValidation;
|
||||||
|
|
||||||
|
namespace Cloud.Validation;
|
||||||
|
|
||||||
|
public class LoginValidator : AbstractValidator<LoginRequest>
|
||||||
|
{
|
||||||
|
public LoginValidator()
|
||||||
|
{
|
||||||
|
RuleFor(request => request.Email)
|
||||||
|
.NotEmpty().WithMessage("Email обязателен для заполнения")
|
||||||
|
.EmailAddress().WithMessage("Некорректный формат Email");
|
||||||
|
|
||||||
|
RuleFor(request => request.Password)
|
||||||
|
.NotEmpty().WithMessage("Пароль обязателен для заполнения")
|
||||||
|
.MinimumLength(8).WithMessage("Пароль должен быть не менее 8 символов");
|
||||||
|
}
|
||||||
|
}
|
22
Cloud/Validation/RegisterValidator.cs
Normal file
22
Cloud/Validation/RegisterValidator.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
using Cloud.Requests;
|
||||||
|
using FluentValidation;
|
||||||
|
|
||||||
|
namespace Cloud.Validation;
|
||||||
|
|
||||||
|
public class RegisterValidator : AbstractValidator<RegisterRequest>
|
||||||
|
{
|
||||||
|
public RegisterValidator()
|
||||||
|
{
|
||||||
|
RuleFor(user => user.Name)
|
||||||
|
.NotEmpty().WithMessage("Имя обязательно для заполнения")
|
||||||
|
.MaximumLength(50).WithMessage("Имя должно быть не более 50 символов");
|
||||||
|
|
||||||
|
RuleFor(user => user.Email)
|
||||||
|
.NotEmpty().WithMessage("Email обязателен для заполнения")
|
||||||
|
.EmailAddress().WithMessage("Некорректный формат Email");
|
||||||
|
|
||||||
|
RuleFor(user => user.Password)
|
||||||
|
.NotEmpty().WithMessage("Пароль обязателен для заполнения")
|
||||||
|
.MinimumLength(8).WithMessage("Пароль должен быть не менее 8 символов");
|
||||||
|
}
|
||||||
|
}
|
8
Cloud/appsettings.Development.json
Normal file
8
Cloud/appsettings.Development.json
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
13
Cloud/appsettings.json
Normal file
13
Cloud/appsettings.json
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
{
|
||||||
|
"Logging": {
|
||||||
|
"LogLevel": {
|
||||||
|
"Default": "Information",
|
||||||
|
"Microsoft.AspNetCore": "Warning"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"AllowedHosts": "*",
|
||||||
|
"Jwt": {
|
||||||
|
"Key": "m7TyhE20s0dVtUDAr9EnFdPZnAG8maxgBTaiW5j6kO6RQhWDAGxYmXyu0suDnE0o",
|
||||||
|
"Issuer": "localhost"
|
||||||
|
}
|
||||||
|
}
|
37
docker-compose.yml
Normal file
37
docker-compose.yml
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
services:
|
||||||
|
cloud:
|
||||||
|
build: ./Cloud/
|
||||||
|
ports:
|
||||||
|
- "5124:5124"
|
||||||
|
depends_on:
|
||||||
|
- postgres
|
||||||
|
- redis
|
||||||
|
postgres:
|
||||||
|
image: postgres:14
|
||||||
|
container_name: cucumber_database
|
||||||
|
environment:
|
||||||
|
POSTGRES_USER: postgres
|
||||||
|
POSTGRES_PASSWORD: 12345
|
||||||
|
POSTGRES_DB: main_database
|
||||||
|
ports:
|
||||||
|
- "5438:5432"
|
||||||
|
volumes:
|
||||||
|
- postgres_data:/var/lib/postgresql/data
|
||||||
|
redis:
|
||||||
|
image: 'redis:latest'
|
||||||
|
ports:
|
||||||
|
- '6379:6379'
|
||||||
|
volumes:
|
||||||
|
- 'cloud-redis:/data'
|
||||||
|
healthcheck:
|
||||||
|
test:
|
||||||
|
- CMD
|
||||||
|
- redis-cli
|
||||||
|
- ping
|
||||||
|
retries: 3
|
||||||
|
timeout: 5s
|
||||||
|
volumes:
|
||||||
|
postgres_data:
|
||||||
|
driver: local
|
||||||
|
cloud-redis:
|
||||||
|
driver: local
|
Loading…
Reference in New Issue
Block a user