Newer
Older
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using AutoMapper;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
using Tsi1.DataLayer;
using Tsi1.DataLayer.Entities;
namespace Tsi1.BusinessLayer.Services
{
public class CommunicationService : ICommunicationService
{
private readonly Tsi1Context _context;
private readonly IMapper _mapper;
public CommunicationService(Tsi1Context context, IMapper mapper)
{
_context = context;
_mapper = mapper;
}
public async Task<ServiceResult<bool>> Create(CommunicationCreateDto newCommunication, int id)
{
var result = new ServiceResult<bool>();
var isGlobal = newCommunication.IsGlobal;
var commmunication = _mapper.Map<Communication>(newCommunication);
var validationResult = await this.CreateValidation(id, isGlobal, commmunication);
if (validationResult.HasError)
{
return validationResult;
}
_context.Communications.Add(commmunication);
await _context.SaveChangesAsync();
return result;
}
public async Task<ServiceResult<bool>> Delete(int communicationId)
{
var result = new ServiceResult<bool>();
var commmunication = await _context.Communications.FirstOrDefaultAsync(x => x.Id == communicationId);
if (commmunication == null)
{
result.HasError = true;
result.AddMessage(string.Format(ErrorMessages.CommunicationDoesNotExist, communicationId));
return result;
}
_context.Communications.Remove(commmunication);
await _context.SaveChangesAsync();
return result;
}
public async Task<ServiceResult<List<CommunicationPreviewDto>>> GetMyCommunications(int userId)
{
var result = new ServiceResult<List<CommunicationPreviewDto>>();
var user = await _context.Users.AsNoTracking().FirstOrDefaultAsync(x => x.Id == userId);
var courseIds = await _context.StudentCourses
.AsNoTracking()
.Where(x => x.StudentId == user.StudentId)
.Select(x => x.CourseId)
.ToListAsync();
var communications = await _context.Communications
.AsNoTracking()
.Include(x => x.Tenant)
.Include(x => x.Course)
.Where(x => (x.Tenant.Id == userId || courseIds.Contains(x.Course.Id))
&& x.ValidUntil >= DateTime.Now)
.ToListAsync();
result.Data = _mapper.Map<List<CommunicationPreviewDto>>(communications);
return result;
}
public async Task<ServiceResult<bool>> TenantValidation(int tenantId, int courseId)
{
var result = new ServiceResult<bool>();
var tenantAdmin = await _context.Tenants.AsNoTracking().FirstOrDefaultAsync(x => x.Name == TenantAdmin.Name);
var course = await _context.Courses.AsNoTracking().FirstOrDefaultAsync(x => x.Id == courseId);
if (tenantAdmin.Id != tenantId)
{
result.HasError = course.TenantId != tenantId;
result.AddMessage(string.Format(ErrorMessages.InvalidTenant, course.TenantId));
}
return result;
}
private async Task<ServiceResult<bool>> CreateValidation(int id, bool isGlobal, Communication commmunication)
{
var result = new ServiceResult<bool>();
if (isGlobal)
{
var tenant = await _context.Tenants.FirstOrDefaultAsync(x => x.Id == id && x.Name != TenantAdmin.Name);
if (tenant == null)
{
result.HasError = true;
result.AddMessage(string.Format(ErrorMessages.TenantDoesNotExist, id));
return result;
}
commmunication.Tenant = tenant;
}
else
{
var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id == id);
if (course == null)
{
result.HasError = true;
result.AddMessage(string.Format(ErrorMessages.CourseDoesNotExist, id));
return result;
}
commmunication.Course = course;
}
return result;
}
}
}