aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio_v2/Tango.BLL/Services/OrganizationsService.cs
blob: 676496832cfe4fb2c5f164dbe5252cdda1e9a9b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Tango.BLL.Objects;
using Tango.DAL;
using Tango.DAL.Entities;
using System.Linq;
using Tango.BLL.Mappers;

namespace Tango.BLL.Services
{
    public class OrganizationsService : ServiceBase<Organization>
    {
        private IRepository<OrganizationEntity> _organizationsRepository;
        private IRepository<MachineEntity> _machinesRepository;
        private OrganizationToOrganizationEntityMapper _mapper;

        public OrganizationsService(IRepository<OrganizationEntity> organizationsRepository, IRepository<MachineEntity> machinesRepository)
        {
            _organizationsRepository = organizationsRepository;
            _machinesRepository = machinesRepository;
            _mapper = new OrganizationToOrganizationEntityMapper();
        }

        public async Task<List<Organization>> GetAllOrganizations()
        {
            var entities = await _organizationsRepository.GetAllAsync();

            var organizations = entities.Select(x => _mapper.Create(x)).ToList();

            return organizations;
        }

        public async Task<long> DeleteAllOrganizations()
        {
            var orgs = await GetAllOrganizations();

            long count = 0;

            foreach (var org in orgs)
            {
                count += await DeleteOrganizationByID(org.ID);
            }

            return count;
        }

        public async Task<Organization> AddOrganization(Organization organization)
        {
            OrganizationEntity entity = _mapper.Create(organization);

            entity = await _organizationsRepository.Insert(entity);

            _mapper.Map(entity, organization);

            return organization;
        }

        public async Task<Organization> GetOrganizationByID(String id)
        {
            var entity = (await _organizationsRepository.GetAsync(x => x.ID == id)).ToList().SingleOrDefault();

            if (entity == null)
            {
                throw new KeyNotFoundException($"Could not find organization with id {id}.");
            }

            return _mapper.Create(entity);
        }

        public async Task<long> DeleteOrganizationByID(String id)
        {
            await _machinesRepository.DeleteAsync(x => x.OrganizationID == id);
            long count = await _organizationsRepository.DeleteAsync(x => x.ID == id);
            return count;
        }

        public async Task<long> Count()
        {
            return await _organizationsRepository.Count();
        }
    }
}