mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
58 lines
2.0 KiB
C#
58 lines
2.0 KiB
C#
using MyCore.Interfaces.DTO;
|
|
using MyCore.Interfaces.Models;
|
|
using MyCore.Services;
|
|
using MyCore.Services.MyControlPanel;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace MyCore.Service
|
|
{
|
|
public class RoomService
|
|
{
|
|
public static RoomDetailDTO CreateOrUpdate(RoomDatabaseService _RoomDatabaseService, DeviceDatabaseService _DeviceDatabaseService, string homeId, RoomCreateOrUpdateDetailDTO roomCreateOrUpdateDetailDTO, bool create)
|
|
{
|
|
List<Device> devices = new List<Device>();
|
|
Room room;
|
|
if (create)
|
|
{
|
|
room = new Room();
|
|
room.CreatedDate = DateTime.Now;
|
|
}
|
|
else
|
|
{
|
|
room = _RoomDatabaseService.GetById(roomCreateOrUpdateDetailDTO.Id);
|
|
}
|
|
|
|
room.HomeId = homeId;
|
|
room.Name = roomCreateOrUpdateDetailDTO.Name;
|
|
room.UpdatedDate = DateTime.Now;
|
|
|
|
List<Device> currentDevices = _DeviceDatabaseService.GetByIds(roomCreateOrUpdateDetailDTO.DeviceIds); // A device cannot have multiple rooms. Filter list
|
|
currentDevices = currentDevices.Where(c => c.RoomId == null).ToList(); // TODO add exception or something
|
|
|
|
if (create)
|
|
{
|
|
room.DevicesIds = currentDevices.Select(c => c.Id).ToList();
|
|
room = _RoomDatabaseService.Create(room);
|
|
}
|
|
else
|
|
{
|
|
room.DevicesIds.AddRange(currentDevices.Select(c => c.Id).ToList());
|
|
room = _RoomDatabaseService.Update(room);
|
|
}
|
|
|
|
foreach (var deviceId in roomCreateOrUpdateDetailDTO.DeviceIds)
|
|
{
|
|
Device device = _DeviceDatabaseService.GetById(deviceId);
|
|
devices.Add(device);
|
|
device.RoomId = room.Id;
|
|
_DeviceDatabaseService.Update(device);
|
|
}
|
|
|
|
return room.ToDTO(devices.Select(d => d.ToDTO()).ToList());
|
|
}
|
|
}
|
|
}
|