This repository was archived by the owner on May 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShiftRepository.cs
More file actions
104 lines (86 loc) · 3.35 KB
/
ShiftRepository.cs
File metadata and controls
104 lines (86 loc) · 3.35 KB
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Data.Models;
using Data.Repositories;
using System.Data.Entity;
using Data.Exceptions;
using System.Data.Entity.Core;
namespace Data.MSSQL.Repositories
{
public class ShiftRepository : IShiftRepository, IDisposable
{
private readonly IShiftPlannerDataContext _context;
public ShiftRepository(IShiftPlannerDataContext context)
{
_context = context;
}
public Shift Create(Shift shift)
{
_context.Shifts.Add(shift);
return _context.SaveChanges() > 0 ? shift : null;
}
public IEnumerable<Shift> Create(IEnumerable<Shift> shifts)
{
_context.Shifts.AddRange(shifts);
return _context.SaveChanges() > 0 ? shifts : null;
}
public void Delete(IEnumerable<Shift> shifts)
{
_context.Shifts.RemoveRange(shifts);
_context.SaveChanges();
}
public void Delete(int id, int organizationId)
{
var shift = _context.Shifts.FirstOrDefault(x => x.Id == id && x.Organization.Id == organizationId);
if (shift == null) throw new ObjectNotFoundException("Could not find a shift corresponding to the given id");
if(shift.CheckIns.Any()) throw new ForbiddenException("You cannot delete a shift that contains checked in employees");
_context.Shifts.Remove(shift);
_context.SaveChanges();
}
public IEnumerable<Shift> ReadFromOrganization(string organizationShortKey)
{
return _context.Shifts
.Where(x => x.Organization.ShortKey == organizationShortKey)
.OrderBy(s => s.Start);
}
public Shift Read(int id, int organizationId)
{
return _context.Shifts
.FirstOrDefault(x => x.Id == id && x.Organization.Id == organizationId);
}
public IEnumerable<Shift> ReadFromOrganization(int organizationId)
{
return _context.Shifts
.Where(x => x.Organization.Id == organizationId)
.Include(x => x.Employees.Select(e => e.Roles))
.OrderBy(s => s.Start);
}
public int Update(Shift shift)
{
var dbShift = _context.Shifts.Single(s => shift.Id == s.Id);
dbShift.Start = shift.Start;
dbShift.End = shift.End;
dbShift.Employees = shift.Employees.Select(e => _context.Employees.Single(em => em.Id == e.Id)).ToList();
dbShift.CheckIns = shift.CheckIns;
return _context.SaveChanges();
}
public IEnumerable<Shift> GetOngoingShifts(int organizationId, DateTime now)
{
var inOneHour = now.AddHours(1);
return _context.Shifts
.Where(s => s.Organization.Id == organizationId && (s.Start < now && s.End > now || (s.Start > now && s.Start < inOneHour)));
}
public bool IsOrganisationOpen(string shortKey)
{
var now = DateTime.Now;
return _context.Shifts
.Any(shift => shift.Organization.ShortKey == shortKey && shift.Start <= now && now <= shift.End && shift.CheckIns.Any());
}
public void Dispose()
{
_context.Dispose();
}
}
}