aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio_v2/Tango.BLL/Services/OrganizationsService.cs
blob: a502849e345e4f7974f1ac63eac8cc4525515772 (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
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> _repository;
        private OrganizationToOrganizationEntityMapper _mapper;

        public OrganizationsService(IRepository<OrganizationEntity> repository)
        {
            _repository = repository;
            _mapper = new OrganizationToOrganizationEntityMapper();
        }

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

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

            return organizations;
        }

        public Task<long> DeleteAllOrganizations()
        {
            return _repository.DeleteAsync(x => true);
        }

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

            entity = await _repository.Insert(entity);

            _mapper.Map(entity, organization);

            return organization;
        }

        public async Task<Organization> GetOrganizationByID(String id)
        {
            var entity = (await _repository.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)
        {
            return await _repository.DeleteAsync(x => x.ID == id);
        }

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