Cucumber/Cloud/Services/Domain/Implement/GreenhouseService.cs

68 lines
2.0 KiB
C#

using Cloud.Models;
using Cloud.Services.Broker;
using Cloud.Services.Broker.Support;
using Microsoft.EntityFrameworkCore;
namespace Cloud.Services.Domain.Implement;
public class GreenhouseService : IGreenhouseService
{
private readonly IBrokerService _brokerService;
private readonly ApplicationContext _context;
public GreenhouseService(IBrokerService brokerService, ApplicationContext context)
{
_context = context;
_brokerService = brokerService;
}
public async Task<Greenhouse> Create(Greenhouse greenhouse)
{
var res = await _context.Greenhouses.AddAsync(greenhouse);
await _context.SaveChangesAsync();
return res.Entity;
}
public async Task<Greenhouse> Delete(int id)
{
var greenhouse = await _context.Greenhouses.FirstOrDefaultAsync(x => x.Id == id);
_context.Greenhouses.Remove(greenhouse);
await _context.SaveChangesAsync();
return greenhouse;
}
public async Task<Greenhouse?> GetGreenhouse(int id)
{
return await _context.Greenhouses.FirstOrDefaultAsync(x => x.Id == id);
}
public async Task<Greenhouse> Update(Greenhouse greenhouse)
{
var res = _context.Greenhouses.Update(greenhouse);
await _context.SaveChangesAsync();
return res.Entity;
}
public async Task<IEnumerable<GreenhouseInfo>?> GetAll(int farmId)
{
// await _changeBrokerIp(farmId);
return _brokerService.WaitMessages<GreenhouseInfo>("data");
}
public async Task<GreenhouseInfo?> GetGreenhouseInfo(int id, int farmId)
{
// await _changeBrokerIp(farmId);
var infos = _brokerService.WaitMessages<GreenhouseInfo>("data");
return infos?.FirstOrDefault(x => x.Id == id);
}
private async Task _changeBrokerIp(int farmId)
{
var farm = await _context.Farms.FirstOrDefaultAsync(x => x.Id == farmId);
_brokerService.ChangeBrokerIp(farm.RaspberryIP);
}
}