< Summary

Class:SVETA.Api.Services.Implements.ImportingExporting.RecordImporterBase
Assembly:SVETA.Api
File(s):/opt/dev/sveta_api_build/SVETA.Api/Services/Implements/ImportingExporting/RecordImporterBase.cs
Covered lines:0
Uncovered lines:40
Coverable lines:40
Total lines:79
Line coverage:0% (0 of 40)
Covered branches:0
Total branches:16
Branch coverage:0% (0 of 16)

Metrics

MethodLine coverage Branch coverage
.ctor(...)0%100%
get_Context()0%100%
get_Database()0%100%
AddNewRecord(...)0%0%
Assert(...)0%0%
TryGetInt(...)0%100%
TryGetDecimal(...)0%100%
TryGetBool(...)0%0%

File(s)

/opt/dev/sveta_api_build/SVETA.Api/Services/Implements/ImportingExporting/RecordImporterBase.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Threading.Tasks;
 5using WinSolutions.Sveta.Common;
 6using WinSolutions.Sveta.Common.Extensions;
 7using WinSolutions.Sveta.Server.Data.DataModel.Contexts;
 8using WinSolutions.Sveta.Server.Data.DataModel.Entities;
 9using WinSolutions.Sveta.Server.Services.Implements;
 10
 11namespace SVETA.Api.Services.Implements.ImportingExporting
 12{
 13    public class RecordImporterBase : SvetaServiceBase
 14    {
 15        private readonly SvetaDbContext _database;
 16
 017        public RecordImporterBase(IAuthenticationService authenticationService, SvetaDbContext database) : base(authenti
 018        {
 019            _database = database;
 020        }
 21
 022        public Dictionary<string, object> Context { get; set; } = new Dictionary<string, object>();
 23
 024        public SvetaDbContext Database => _database;
 25
 026        RecordsState inactiveState = null;
 27
 28        public void AddNewRecord(BaseRecord record)
 029        {
 030            inactiveState ??= Database.refRecordsState.Where(x => x.Code == "Inactive").Single();
 031            record.RecState = inactiveState;
 032            Database.Add(record);
 033        }
 34
 35        public void Assert(bool condition, string errorMessage)
 036        {
 037            if(!condition)
 038            {
 039                throw new ArgumentException(errorMessage);
 40            }
 041        }
 42
 43        public int TryGetInt(string value, string errorMessage)
 044        {
 45            try
 046            {
 047                return (int)value.ToDecimal();
 48            }
 049            catch
 050            {
 051                throw new ArgumentException(errorMessage);
 52            }
 053        }
 54
 55        public decimal TryGetDecimal(string value, string errorMessage)
 056        {
 57            try
 058            {
 059                return value.ToDecimal();
 60            }
 061            catch
 062            {
 063                throw new ArgumentException(errorMessage);
 64            }
 065        }
 66
 67        public bool TryGetBool(string value, string errorMessage)
 068        {
 069            value = (value ?? "").ToLower();
 070            if (value == bool.FalseString.ToLower()
 071                || value == bool.TrueString.ToLower()
 072                || value == "1" || value == "0")
 073            {
 074                return value == bool.TrueString.ToLower() || value == "1";
 75            }
 076            throw new ArgumentException(errorMessage);
 077        }
 78    }
 79}