< Summary

Information
Class: WebApi.DAO.SensorGoalDAO
Assembly: IndeklimaWebApi
File(s): /home/runner/work/SEP4/SEP4/backend/microservices/indeklima/WebApi/DAO/SensorGoalDAO.cs
Line coverage
78%
Covered lines: 33
Uncovered lines: 9
Coverable lines: 42
Total lines: 75
Line coverage: 78.5%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
GetSensorGoalAsync()100%1.08157.14%
AddOrUpdateSensorGoalAsync()100%2.01286.95%
DeleteSensorGoalAsync()100%1.05162.5%

File(s)

/home/runner/work/SEP4/SEP4/backend/microservices/indeklima/WebApi/DAO/SensorGoalDAO.cs

#LineLine coverage
 1using MongoDB.Driver;
 2using WebApi.Models;
 3
 4namespace WebApi.DAO
 5{
 6    public class SensorGoalDAO : ISensorGoalDAO
 7    {
 8        private readonly IMongoCollection<SensorGoal> _sensorGoalCollection;
 9
 410        public SensorGoalDAO(MongoDbContext context)
 411        {
 412            _sensorGoalCollection = context.Database.GetCollection<SensorGoal>("SensorGoals");
 413        }
 14
 15        public async Task<SensorGoal?> GetSensorGoalAsync(int hallId)
 416        {
 17            try
 418            {
 419                return await _sensorGoalCollection.Find(g => g.HallId == hallId).FirstOrDefaultAsync();
 20            }
 021            catch (Exception ex)
 022            {
 023                throw new Exception($"Error retrieving sensor goal: {ex.Message}");
 24            }
 425        }
 26
 27        public async Task AddOrUpdateSensorGoalAsync(SensorGoal sensorGoal)
 528        {
 29            try
 530            {
 31                // Find the existing goal by hallId
 532                var existingGoal = await _sensorGoalCollection.Find(g => g.HallId == sensorGoal.HallId)
 533                    .FirstOrDefaultAsync();
 34
 535                if (existingGoal != null)
 136                {
 37                    // Update fields while retaining the original Id
 138                    existingGoal.DesiredTemperature = sensorGoal.DesiredTemperature;
 139                    existingGoal.DesiredHumidity = sensorGoal.DesiredHumidity;
 140                    existingGoal.DesiredCo2 = sensorGoal.DesiredCo2;
 41
 42                    // Replace the existing document with the updated one
 143                    await _sensorGoalCollection.ReplaceOneAsync(
 144                        g => g.Id == existingGoal.Id,
 145                        existingGoal,
 146                        new ReplaceOptions { IsUpsert = true }
 147                    );
 148                }
 49                else
 450                {
 51                    // Insert as new document if no existing document is found
 452                    await _sensorGoalCollection.InsertOneAsync(sensorGoal);
 453                }
 554            }
 055            catch (Exception ex)
 056            {
 057                throw new Exception($"Error adding or updating sensor goal: {ex.Message}");
 58            }
 59
 560        }
 61
 62        public async Task DeleteSensorGoalAsync(int hallId)
 163        {
 64            try
 165            {
 166                await _sensorGoalCollection.DeleteOneAsync(g => g.HallId == hallId);
 167            }
 068            catch (Exception ex)
 069            {
 070                throw new Exception($"Error deleting sensor goal: {ex.Message}");
 71            }
 72
 173        }
 74    }
 75}