예제 #1
0
        /// <summary>
        /// Method creates an pupilAttendance record for the selected pupil
        /// </summary>
        /// <param name="pupilId"></param>
        /// <param name="attendanceDate"></param>
        /// <returns>
        /// int signifying success (1) or failure (0) of operation
        /// </returns>
        public int CreatePupilAttendane(int pupilId, DateTime attendanceDate)
        {
            /* Need to check the database to see if an attendance exists for the selected date and is associated to the pupil's class
             * Get the id of the attendance if it exists and create the pupilAttendance
             * If no attendance exists for the date and class selected we need to create one for that date and then create the pupilAttendance
             */
            Pupil selectedPupil     = GetPupilDetails(pupilId);
            int   attendanceCreated = 0;

            if (smaDB.Attendances.Any(a => DbFunctions.TruncateTime(a.AttendanceDate) == DbFunctions.TruncateTime(attendanceDate) && a.ClassId == selectedPupil.ClassId))
            {
                // An attendance exists for the date and class
                var attendance = smaDB.Attendances.Where(a => DbFunctions.TruncateTime(a.AttendanceDate) == DbFunctions.TruncateTime(attendanceDate) && a.ClassId == selectedPupil.ClassId).FirstOrDefault();
                smaDB.PupilAttendances.Add(new PupilAttendance
                {
                    AttendanceId = attendance.AttendanceId,
                    PupilId      = pupilId
                });
                attendanceCreated = smaDB.SaveChanges();
            }
            else
            {
                // No attendance exists for the date and class
                smaDB.Attendances.Add(new Attendance
                {
                    AttendanceDate = attendanceDate,
                    ClassId        = selectedPupil.ClassId
                });
                smaDB.SaveChanges();

                // Create the pupilAttendance for the selected date
                var attendance = smaDB.Attendances.Where(a => DbFunctions.TruncateTime(a.AttendanceDate) == DbFunctions.TruncateTime(attendanceDate) && a.ClassId == selectedPupil.ClassId).FirstOrDefault();
                smaDB.PupilAttendances.Add(new PupilAttendance
                {
                    AttendanceId = attendance.AttendanceId,
                    PupilId      = pupilId
                });
                attendanceCreated = smaDB.SaveChanges();
            }

            return(attendanceCreated);
        }