Example #1
0
        public async void NFCScanController_PostScan_ShouldMarkTheLectureAsVisited()
        {
            //Arrange
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseMySql(ConnectionString)
                          .Options;
            var context    = new ApplicationDbContext(options);
            var controller = new NFCScanController(context);

            var scan = new NFCScan()
            {
                ScanId    = context.NFCScans.Max(x => x.ScanId) + 1,
                UID       = 190704260,
                TimeStamp = new DateTime(2019, 3, 14, 15, 35, 0),
                DeviceId  = 19
            };

            //Act
            await controller.PostScan(scan);

            var markedAsVisited = context.Timetables.Where(x => x.TimetableId == 5632).First().isVisited;

            //Assert
            Assert.True(markedAsVisited);

            //Deletes created fake scan
            context.NFCScans.Remove(scan);
            //Makes that lecture not visited again
            context.Timetables.Where(x => x.TimetableId == 5632).First().isVisited = false;
            context.SaveChanges();
        }
Example #2
0
        public async Task <ActionResult <NFCScan> > PostScan(NFCScan item)
        {
            _context.NFCScans.Add(item);
            await _context.SaveChangesAsync();

            //System.Diagnostics.Debug.WriteLine(item.ToString());

            return(CreatedAtAction(nameof(GetScan), new { id = item.ScanId }, item));
        }
        public async Task <ActionResult <NFCScan> > PostScan(NFCScan item)
        {
            // Find related timetable element by deviceId, UID and time
            // Find classroom
            var classRoom = await _context.Devices.Where(d => d.DeviceIdReal == item.DeviceId).FirstOrDefaultAsync();

            // Find student
            var stud = await _context.Users.Where(u => u.UID == item.UID).FirstOrDefaultAsync();

            // Find period
            var period = await _context.Periods
                         .Where(
                p => TimeSpan.Compare(p.PeriodStartTime, item.TimeStamp.TimeOfDay) == -1 &&
                TimeSpan.Compare(p.PeriodEndTime, item.TimeStamp.TimeOfDay) == 1)
                         .FirstOrDefaultAsync();

            // Find exact timetable
            if (stud != null && period != null && classRoom != null)
            {
                var timetable = await _context.Timetables
                                .Where(
                    t => t.ClassroomId == classRoom.ClassroomId &&
                    t.StudentId == stud.Id &&
                    t.Date.Date == item.TimeStamp.Date &&
                    t.PeriodId == period.PeriodId)
                                .FirstOrDefaultAsync();

                // Mark visit
                if (timetable != null)
                {
                    timetable.isVisited = true;
                    _context.Timetables.Update(timetable);
                }
            }
            _context.NFCScans.Add(item);
            await _context.SaveChangesAsync();

            //System.Diagnostics.Debug.WriteLine(item.ToString());

            return(CreatedAtAction(nameof(GetScan), new { id = item.ScanId }, item));
        }