fix: mac теперь не ипользуется. ip наше все

This commit is contained in:
mfnefd 2024-11-12 19:18:37 +04:00
parent c4899d4a11
commit 03302065ab
4 changed files with 129 additions and 130 deletions

View File

@ -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<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");
[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);
}
}
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);
[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");
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<ActionResult<Farm>> 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<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();
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<ActionResult<Farm>> 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<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");
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<ActionResult> 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<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");
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);
}
}
}
}

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -3,16 +3,16 @@ using FluentValidation;
namespace Cloud.Validation
{
public class FarmValidator : AbstractValidator<FarmRequest>
{
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<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");
}
}
RuleFor(request => request.Name)
.NotEmpty().WithMessage("Name can't be empty");
}
}
}