public bool UpdateClassTimetable(ClassTimetableDto dto)
        {
            try
            {
                var classTimetable = _context.ClassTimetable.FirstOrDefault(x => x.Id == dto.Id);

                var classType = _context.ClassTypes.FirstOrDefault(x => x.Name == dto.ClassTypeName);

                if (classType == null || classTimetable == null)
                {
                    _logger.Warn($"ClassTimetable ID {dto.Id} or ClassType name {dto.ClassTypeName} not found in database");
                    return(false);
                }

                classTimetable.ClassType   = classType;
                classTimetable.ClassTypeId = classType.Id;
                classTimetable.StartTime   = new DateTime(2000, 1, 1, dto.StartHour, dto.StartMinutes, 0);
                classTimetable.EndTime     = new DateTime(2000, 1, 1, dto.EndHour, dto.EndMinutes, 0);
                classTimetable.Weekday     = dto.Weekday;

                _context.Entry(classTimetable).State = EntityState.Modified;
                _context.SaveChanges();

                _logger.Info($"ClassTimetable ID {dto.Id} updated");

                return(true);
            }
            catch (Exception e)
            {
                _logger.Error(e);
                return(false);
            }
        }
Ejemplo n.º 2
0
        public bool UpdateHoliday(HolidayDto dto)
        {
            try
            {
                if (dto == null)
                {
                    _logger.Warn("Parameter passed is null");
                    return(false);
                }

                var holiday = _context.Holiday.FirstOrDefault(x => x.HolidaysId == dto.Id);

                if (holiday == null)
                {
                    _logger.Warn($"Holiday ID {dto.Id} not found in database");
                    return(false);
                }

                holiday.Name        = dto.Name;
                holiday.HolidayDate = dto.HolidayDate;

                _context.Entry(holiday).State = EntityState.Modified;
                _context.SaveChanges();

                _logger.Info($"Holiday {dto.Name} on {dto.HolidayDate} has been updated");

                return(true);
            }
            catch (Exception e)
            {
                _logger.Error(e);
                return(false);
            }
        }
        public bool UpdateScheduledClass(ScheduledClassDto dto)
        {
            try
            {
                if (dto == null)
                {
                    _logger.Warn("Parameter passed is null");
                    return(false);
                }

                var scheduledClass = _context.ScheduledClasses
                                     .Include("ClassType")
                                     .Include("Instructor")
                                     .FirstOrDefault(x => x.Id == dto.Id);

                if (scheduledClass.ClassType.Name != dto.ClassTypeName)
                {
                    var classType = _context.ClassTypes.FirstOrDefault(x => x.Name == dto.ClassTypeName);
                    if (classType == null)
                    {
                        _logger.Warn($"ClassType {dto.ClassTypeName} not found in database");
                        return(false);
                    }

                    scheduledClass.ClassType   = classType;
                    scheduledClass.ClassTypeId = classType.Id;
                    _logger.Info($"ScheduledClass ID {scheduledClass.Id} ClassType has been changed to {dto.ClassTypeName}");
                }

                if (scheduledClass.Instructor.UserName != dto.InstructorName)
                {
                    var instructor = _context.Users.FirstOrDefault(x => x.UserName == dto.InstructorName);
                    if (instructor == null)
                    {
                        _logger.Warn($"Instructor {dto.InstructorName} not found in database");
                        return(false);
                    }

                    scheduledClass.Instructor   = instructor;
                    scheduledClass.InstructorId = instructor.Id;
                    _logger.Info($"ScheduledClass ID {dto.Id} has been changed to {dto.InstructorName}");
                }

                scheduledClass.IsCancelled    = dto.IsCancelled;
                scheduledClass.ClassStartTime = dto.ClassStartTime;

                _context.Entry(scheduledClass).State = EntityState.Modified;
                _context.SaveChanges();

                _logger.Info($"ScheduledClass ID {dto.Id} has been updated");

                return(true);
            }
            catch (Exception e)
            {
                _logger.Error(e);
                return(false);
            }
        }
Ejemplo n.º 4
0
        public bool EditUser(GymUserDto dto)
        {
            try
            {
                if (dto == null)
                {
                    _logger.Warn("Parameter passed is null");
                    return(false);
                }

                var userManager = new UserManager <GymUser>(new UserStore <GymUser>(_context));

                //var user = _context.Users.FirstOrDefault(x => x.Id == gymUser.Id);
                var user = userManager.Users.FirstOrDefault(x => x.Id == dto.Id);

                if (user == null)
                {
                    _logger.Warn($"Gymuser ID {dto.Id} not found in database");
                    return(false);
                }

                // todo: replace with automapper
                user.DateModified = DateTime.UtcNow;
                user.Email        = dto.Email;
                user.FirstName    = dto.FirstName;
                user.LastName     = dto.LastName;
                user.UserName     = dto.UserName;

                var currentRoleId   = user.Roles.FirstOrDefault().RoleId;
                var currentRoleName = _context.Roles.FirstOrDefault(x => x.Id == currentRoleId).Name;

                if (currentRoleName != dto.RoleName)
                {
                    userManager.RemoveFromRole(user.Id, currentRoleName);
                    userManager.AddToRole(user.Id, dto.RoleName);
                    _logger.Info($"Gymuser ID {dto.Id} has changed role from {currentRoleName} to {dto.RoleName}");
                }


                // todo: might have to do something with password soon

                _context.Entry(user).State = System.Data.Entity.EntityState.Modified;
                _context.SaveChanges();

                _logger.Info($"Gymuser ID {dto.Id} has been updated");

                return(true);
            }
            catch (Exception e)
            {
                _logger.Error(e);
                return(false);
            }
        }
Ejemplo n.º 5
0
        public bool UpdateClassAttendance(ClassAttendanceDto dto)
        {
            try
            {
                if (dto == null)
                {
                    _logger.Warn($"Parameter id passed is null");
                    return(false);
                }

                var classAttendance = _context.ClassAttendances.FirstOrDefault(x => x.Id == dto.Id);

                if (classAttendance == null)
                {
                    _logger.Warn($"ClassAttendance ID {dto.Id} not found in database");
                    return(false);
                }

                if (classAttendance.AttendeeId != dto.AttendeeId)
                {
                    var attendee = _context.Users.FirstOrDefault(x => x.Id == dto.AttendeeId);

                    classAttendance.Attendee   = attendee;
                    classAttendance.AttendeeId = dto.AttendeeId;
                }

                if (classAttendance.ScheduledClassId != dto.ScheduledClassId)
                {
                    var scheduledClass = _context.ScheduledClasses.FirstOrDefault(x => x.Id == dto.ScheduledClassId);

                    classAttendance.ScheduledClass   = scheduledClass;
                    classAttendance.ScheduledClassId = dto.ScheduledClassId;
                }

                classAttendance.EnrolledBy   = dto.EnrolledBy;
                classAttendance.EnrolledDate = dto.EnrolledDate;
                classAttendance.NoShow       = dto.NoShow;

                _context.Entry(classAttendance).State = EntityState.Modified;
                _context.SaveChanges();

                return(true);
            }
            catch (Exception e)
            {
                _logger.Error(e);
                return(false);
            }
        }
Ejemplo n.º 6
0
        public bool EditClassType(ClassTypeDto dto)
        {
            try
            {
                if (dto.Id == null)
                {
                    _logger.Warn($"Paramater's ID {dto.Id} passed is null");
                    return(false);
                }

                var classType = _context.ClassTypes.FirstOrDefault(x => x.Id == dto.Id);

                if (classType != null)
                {
                    classType.IsActive      = dto.IsActive;
                    classType.Name          = dto.Name;
                    classType.Difficulty    = dto.Difficulty;
                    classType.Description   = dto.Description;
                    classType.ClassColour   = dto.ClassColour;
                    classType.ImageFileName = dto.ImageFileName;

                    _context.Entry(classType).State = System.Data.Entity.EntityState.Modified;
                    _context.SaveChanges();

                    _logger.Info($"ClassTimetable ID {dto.Id} updated");

                    return(true);
                }

                _logger.Warn($"Classtype ID {dto.Id} not found in database");
                return(false);
            }
            catch (Exception e)
            {
                _logger.Error(e);
                return(false);
            }
        }