Ejemplo n.º 1
0
        public async Task <ApiResult <List <ShiftReadEditDto> > > AddShifts(List <ShiftReadEditDto> dtos)
        {
            try
            {
                var shifts = _mapper.Map <List <Shift> >(dtos);
                foreach (var shift in shifts)
                {
                    var position = await _context.Positions.FirstOrDefaultAsync(p => p.Id == shift.PositionId);

                    var member = await _context.Members.FirstOrDefaultAsync(m => m.Id == shift.MemberProfileId);

                    // when a shift that has been edited from a recurring set is added, its Position is outdated at this moment, so null it out.
                    // not doing so will cause a unique constraint error in the positions table
                    shift.Position = null;

                    shift.Subject = member != null ?
                                    position.Name + " - " + member.FirstName + " " + member.LastName :
                                    position.Name + " - Open ";

                    // this must be set back to null to avoid foreign key constraint
                    if (member == null)
                    {
                        shift.MemberProfileId = null;
                    }
                }

                _context.AddRange(shifts);

                await _context.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                _logger.LogError("An error occured when trying to add a new shift:\n" + ex.Message + "\n\nStack Trace: " + ex.StackTrace);

                return(new ApiResult <List <ShiftReadEditDto> >
                {
                    Successful = false,
                    Error = "An error occurred when trying to add a new shift, please try again."
                });
            }

            return(await GetShifts());
        }
Ejemplo n.º 2
0
        public ApiResult <object> SeedShifts()
        {
            try
            {
                if (_context.Shifts.Any())
                {
                    return(new ApiResult <object>()
                    {
                        Successful = true
                    });
                }

                bool membersSeeded   = _context.Members.Any();
                bool positionsSeeded = _context.Positions.Any();

                if (!(membersSeeded && positionsSeeded))
                {
                    return(new ApiResult <object>()
                    {
                        Successful = false, Error = "Members and positions must be seeded before shifts."
                    });
                }

                var members = _context.Members.ToList();

                var shifts = new List <Shift>();

                int dayOfMonth = 1;
                foreach (var member in members)
                {
                    shifts.Add(new Shift()
                    {
                        Member    = member,
                        Position  = _context.Positions.FirstOrDefault(),
                        StartTime = new DateTime(1, 1, dayOfMonth, 8, 0, 0),
                        EndTime   = new DateTime(1, 1, dayOfMonth, 14, 0, 0),
                        Subject   = "Test Shift",
                    });

                    dayOfMonth++;
                }

                _context.AddRange(shifts);
                _context.SaveChanges();

                return(new ApiResult <object>()
                {
                    Successful = true
                });
            }
            catch (Exception ex)
            {
                string errorString = $"Something went wrong when trying to seed shifts in the database:\nError:{ex.Message}";
                if (ex.Message.ToLower().Contains("inner exception"))
                {
                    errorString += $"\nInner Exception: {ex.InnerException}";
                }
                errorString += $"\nInner Exception: {ex.StackTrace}";

                _logger.LogError(errorString);
                return(new ApiResult <object>()
                {
                    Error = "Something went wrong when trying to seed shifts in the database.", Successful = false
                });
            }
        }