コード例 #1
0
ファイル: SessionsController.cs プロジェクト: a2937/wvup-tcs
        public async Task<IActionResult> PostSession([FromBody] SessionPostOrPutDTO sessionDTO)
        {
            var person = await _personRepo.Find(x => x.Id == sessionDTO.PersonId);
            if (person.PersonType == PersonType.Teacher)
            {
                var sessionTeacher = _mapper.Map<Session>(sessionDTO);
                await _sessionRepo.Create(sessionTeacher);
                return Created($"sessions/{sessionTeacher.Id}", sessionTeacher);
            }

            if (sessionDTO.SelectedClasses.Count < 1)
            {
                throw new TCSException("Must select at least one class.");
            }

            if (sessionDTO.SelectedReasons.Count < 1 && !sessionDTO.Tutoring)
            {
                throw new TCSException("Must select at least one reason for visit.");
            }

            var session = _mapper.Map<Session>(sessionDTO);
            session.SessionClasses = sessionDTO.SelectedClasses.Select(x => new SessionClass() { ClassId = x }).ToList();
            session.SessionReasons = sessionDTO.SelectedReasons.Select(x => new SessionReason() { ReasonId = x }).ToList();
            await _sessionRepo.Create(session);

            return Created($"sessions/{session.Id}", session);
        }
コード例 #2
0
        public async void POST_Sessions_ShouldReturn201WithCreatedSession()
        {
            var client  = _factory.CreateClient();
            var session = new SessionPostOrPutDTO()
            {
                InTime          = DateTime.Now,
                OutTime         = DateTime.Now.Add(new TimeSpan(1)),
                PersonId        = 1,
                Tutoring        = true,
                SelectedClasses = new List <int> ()
                {
                    3, 4
                },
                SelectedReasons = new List <int> ()
                {
                    2
                },
                SemesterCode = 201901
            };
            var user = await Login(client);

            var request = new HttpRequestMessage(HttpMethod.Post, "api/sessions");

            request.Headers.Add("Authorization", $"Bearer {user.Token}");
            request.Content = new StringContent(JsonConvert.SerializeObject(session));
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var response = await client.SendAsync(request);

            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
            var createdSession = await response.Content.ReadAsAsync <Session> ();

            Assert.Equal(1, createdSession.PersonId);
            Assert.True(session.Tutoring);
        }
コード例 #3
0
ファイル: SessionsController.cs プロジェクト: a2937/wvup-tcs
        public async Task<IActionResult> UpdateSession([FromRoute] int id, [FromBody] SessionPostOrPutDTO sessionDTO)
        {
            var person = await _personRepo.Find(x => x.Id == sessionDTO.PersonId);
            var isTeacher = person.PersonType == PersonType.Teacher;

            if (id != sessionDTO.Id)
            {
                throw new TCSException("Session does not exist");
            }

            if (!isTeacher)
            {

                if (sessionDTO.SelectedClasses.Count < 1)
                {
                    throw new TCSException("Must select at least one class.");
                }

                if (sessionDTO.SelectedReasons.Count < 1 && !sessionDTO.Tutoring)
                {
                    throw new TCSException("Must select at least one reason for visit.");
                }
            }

            try
            {
                var session = new Session()
                {
                    Id = sessionDTO.Id,
                    InTime = sessionDTO.InTime,
                    OutTime = sessionDTO.OutTime,
                    PersonId = sessionDTO.PersonId,
                    SemesterCode = sessionDTO.SemesterCode,
                    Tutoring = sessionDTO.Tutoring,
                };

                await _sessionRepo.Update(session);
                if (!isTeacher)
                {
                    await _sessionClassRepo.RemoveAll(x => x.SessionId == sessionDTO.Id);
                    foreach (var x in sessionDTO.SelectedClasses)
                    {
                        await _sessionClassRepo.Create(new SessionClass() { SessionId = sessionDTO.Id, ClassId = x });
                    }
                    await _sessionReasonRepo.RemoveAll(x => x.SessionId == sessionDTO.Id);
                    foreach (var x in sessionDTO.SelectedReasons)
                    {
                        await _sessionReasonRepo.Create(new SessionReason() { SessionId = sessionDTO.Id, ReasonId = x });
                    }
                }
                var updatedSession = await _sessionRepo.Find(x => x.Id == sessionDTO.Id);
                return Ok(updatedSession);
            }
            catch (DbUpdateConcurrencyException)
            {
                return NotFound(new { message = "Something went wrong" });
            }
        }
コード例 #4
0
        public async void UpdateSession_TeacherSession_SubmitWithNoClassesAndReasons_ShouldSaveSelectedIntoDatabase()
        {
            var person = new Person()
            {
                Email      = "*****@*****.**",
                FirstName  = "Teach",
                LastName   = "Pecan",
                Id         = 7981,
                PersonType = PersonType.Teacher
            };
            var semester = new Semester()
            {
                Code = 201903
            };

            var session = new Session()
            {
                InTime       = DateTime.Now,
                OutTime      = DateTime.Now.Add(new TimeSpan(5)),
                PersonId     = person.Id,
                SemesterCode = semester.Code
            };

            db.People.Add(person);
            db.Semesters.Add(semester);
            db.Sessions.Add(session);
            db.SaveChanges();

            var sessionUpdate = new SessionPostOrPutDTO()
            {
                InTime       = session.InTime,
                OutTime      = session.OutTime.Value.Add(new TimeSpan(5)),
                PersonId     = session.PersonId,
                Id           = session.Id,
                SemesterCode = session.SemesterCode,
                Tutoring     = session.Tutoring,
            };

            var result = await sessionController.UpdateSession(sessionUpdate.Id, sessionUpdate);

            var dbSession = await sessionRepo.Find(x => x.Id == sessionUpdate.Id);

            Assert.Equal(sessionUpdate.OutTime, dbSession.OutTime);
        }
コード例 #5
0
        public async void POST_Sessions_TeacherSession_ShouldReturn201()
        {
            var client  = _factory.CreateClient();
            var session = new SessionPostOrPutDTO()
            {
                InTime       = DateTime.Now,
                OutTime      = DateTime.Now.Add(new TimeSpan(1)),
                PersonId     = 771771771,
                SemesterCode = 201901
            };
            var user = await Login(client);

            var request = new HttpRequestMessage(HttpMethod.Post, "api/sessions");

            request.Headers.Add("Authorization", $"Bearer {user.Token}");
            request.Content = new StringContent(JsonConvert.SerializeObject(session));
            request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var response = await client.SendAsync(request);

            Assert.Equal(HttpStatusCode.Created, response.StatusCode);
        }
コード例 #6
0
        public async void UpdateSession_StudentSession_CompletelyChangeClassesAndReasons_ShouldSaveSelectedIntoDatabase()
        {
            var classes = new List <Class>()
            {
                new Class()
                {
                    Name       = "Into to Excel",
                    ShortName  = "CS 101",
                    CRN        = 24668,
                    Department = new Department()
                    {
                        Code = 134,
                        Name = "STEM"
                    }
                },
                new Class()
                {
                    Name       = "Advanced Datastructures",
                    ShortName  = "CS 387",
                    CRN        = 546789,
                    Department = new Department()
                    {
                        Code = 134,
                        Name = "STEM"
                    }
                }
            };
            var reasons = new List <Reason>()
            {
                new Reason()
                {
                    Id   = 1,
                    Name = "Bone Use"
                },
                new Reason()
                {
                    Id   = 2,
                    Name = "Computer Use"
                }
            };

            var person = new Person()
            {
                Email      = "*****@*****.**",
                FirstName  = "Tom",
                LastName   = "Betty",
                Id         = 4697,
                PersonType = PersonType.Student
            };
            var semester = new Semester()
            {
                Code = 201903
            };

            var session = new Session()
            {
                InTime         = DateTime.Now,
                OutTime        = DateTime.Now.Add(new TimeSpan(5)),
                PersonId       = person.Id,
                SemesterCode   = semester.Code,
                SessionClasses = new List <SessionClass>()
                {
                    new SessionClass()
                    {
                        ClassId = 24668
                    }
                },
                SessionReasons = new List <SessionReason>()
                {
                    new SessionReason()
                    {
                        ReasonId = 1
                    }
                }
            };

            db.Classes.AddRange(classes);
            db.People.Add(person);
            db.Semesters.Add(semester);
            db.Sessions.Add(session);
            db.SaveChanges();

            var sessionUpdate = new SessionPostOrPutDTO()
            {
                InTime          = session.InTime,
                OutTime         = session.OutTime.Value,
                PersonId        = session.PersonId,
                Id              = session.Id,
                SemesterCode    = session.SemesterCode,
                Tutoring        = session.Tutoring,
                SelectedClasses = new List <int>()
                {
                    546789
                },
                SelectedReasons = new List <int>()
                {
                    2
                }
            };

            await sessionController.UpdateSession(sessionUpdate.Id, sessionUpdate);

            var sessionClasses = db.SessionClasses.Where(x => x.SessionId == session.Id);
            var sessionReasons = db.SessionReasons.Where(x => x.SessionId == session.Id);

            Assert.Equal(sessionUpdate.SelectedClasses, sessionClasses.Select(x => x.ClassId).ToList());
            Assert.Equal(sessionUpdate.SelectedReasons, sessionReasons.Select(x => x.ReasonId).ToList());
        }