| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using WinSolutions.Sveta.Common; |
| | | 6 | | using WinSolutions.Sveta.Common.Extensions; |
| | | 7 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 8 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 9 | | using WinSolutions.Sveta.Server.Services.Implements; |
| | | 10 | | |
| | | 11 | | namespace SVETA.Api.Services.Implements.ImportingExporting |
| | | 12 | | { |
| | | 13 | | public class RecordImporterBase : SvetaServiceBase |
| | | 14 | | { |
| | | 15 | | private readonly SvetaDbContext _database; |
| | | 16 | | |
| | 0 | 17 | | public RecordImporterBase(IAuthenticationService authenticationService, SvetaDbContext database) : base(authenti |
| | 0 | 18 | | { |
| | 0 | 19 | | _database = database; |
| | 0 | 20 | | } |
| | | 21 | | |
| | 0 | 22 | | public Dictionary<string, object> Context { get; set; } = new Dictionary<string, object>(); |
| | | 23 | | |
| | 0 | 24 | | public SvetaDbContext Database => _database; |
| | | 25 | | |
| | 0 | 26 | | RecordsState inactiveState = null; |
| | | 27 | | |
| | | 28 | | public void AddNewRecord(BaseRecord record) |
| | 0 | 29 | | { |
| | 0 | 30 | | inactiveState ??= Database.refRecordsState.Where(x => x.Code == "Inactive").Single(); |
| | 0 | 31 | | record.RecState = inactiveState; |
| | 0 | 32 | | Database.Add(record); |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | public void Assert(bool condition, string errorMessage) |
| | 0 | 36 | | { |
| | 0 | 37 | | if(!condition) |
| | 0 | 38 | | { |
| | 0 | 39 | | throw new ArgumentException(errorMessage); |
| | | 40 | | } |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | public int TryGetInt(string value, string errorMessage) |
| | 0 | 44 | | { |
| | | 45 | | try |
| | 0 | 46 | | { |
| | 0 | 47 | | return (int)value.ToDecimal(); |
| | | 48 | | } |
| | 0 | 49 | | catch |
| | 0 | 50 | | { |
| | 0 | 51 | | throw new ArgumentException(errorMessage); |
| | | 52 | | } |
| | 0 | 53 | | } |
| | | 54 | | |
| | | 55 | | public decimal TryGetDecimal(string value, string errorMessage) |
| | 0 | 56 | | { |
| | | 57 | | try |
| | 0 | 58 | | { |
| | 0 | 59 | | return value.ToDecimal(); |
| | | 60 | | } |
| | 0 | 61 | | catch |
| | 0 | 62 | | { |
| | 0 | 63 | | throw new ArgumentException(errorMessage); |
| | | 64 | | } |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | public bool TryGetBool(string value, string errorMessage) |
| | 0 | 68 | | { |
| | 0 | 69 | | value = (value ?? "").ToLower(); |
| | 0 | 70 | | if (value == bool.FalseString.ToLower() |
| | 0 | 71 | | || value == bool.TrueString.ToLower() |
| | 0 | 72 | | || value == "1" || value == "0") |
| | 0 | 73 | | { |
| | 0 | 74 | | return value == bool.TrueString.ToLower() || value == "1"; |
| | | 75 | | } |
| | 0 | 76 | | throw new ArgumentException(errorMessage); |
| | 0 | 77 | | } |
| | | 78 | | } |
| | | 79 | | } |