using AutoMapper; using PARR.Core.Repositories.Interfaces.RobotRepositories; using PARR.Domain.DTOs.RobotSnapshotDTO; using PARR.Domain.Entities.RobotEntities; using PARR.Domain.Exceptions; namespace PARR.Core.Services.RobotSnapshotServices { internal class RobotSnapshotService : IRobotSnapshotService { private readonly IRobotSnapshotRepository robotSnapshotRepository; private readonly IMapper mapper; public RobotSnapshotService( IRobotSnapshotRepository robotSnapshotRepository, IMapper mapper ) { this.robotSnapshotRepository = robotSnapshotRepository; this.mapper = mapper; } public async Task CreateAsync(CreateRobotSnapshot robotSnapshot) { var snapshot = new RobotSnapshot { Id = Guid.NewGuid(), TemplateRobotsCount = robotSnapshot.TemplateRobotsCount, ScheduleRobotsCount = robotSnapshot.ScheduleRobotsCount, MaxRobots = robotSnapshot.MaxRobots, Ip = robotSnapshot.Ip }; var createdResult = await robotSnapshotRepository.CreateAsync(snapshot); var commitResult = await robotSnapshotRepository.CommitAsync(); if (!createdResult || !commitResult) throw new DbErrorException("Ошибка сохранения в БД"); return mapper.Map(snapshot); } } }