Example #1
0
        public async Task <IActionResult> RegisterTestAttendeesAsync([FromBody] TestAttendees testAttendee, [FromRoute] string magicString)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var test = await _testRepository.GetTestByLinkAsync(magicString);

            //If test link is invalid
            if (test == null)
            {
                return(BadRequest());
            }

            testAttendee.TestId = test.Id;

            var dbTestAttendee = await _testConductRepository.GetTestAttendeeByEmailIdAndRollNo(testAttendee.Email, testAttendee.RollNumber, testAttendee.TestId);

            //If attendee doesnt exist add him
            if (dbTestAttendee == null)
            {
                await _testConductRepository.RegisterTestAttendeesAsync(testAttendee);

                HttpContext.Session.SetInt32(_stringConstants.AttendeeIdSessionKey, testAttendee.Id);
                return(Ok(testAttendee));
            }

            //If attendee exist
            else
            {
                var testStatus = await _testConductRepository.GetAttendeeTestStatusAsync(dbTestAttendee.Id);

                //Then check his status
                if (testStatus != TestStatus.AllCandidates)
                {
                    return(NotFound());
                }
                //If status is first one then just set session and return him
                else
                {
                    HttpContext.Session.SetInt32(_stringConstants.AttendeeIdSessionKey, dbTestAttendee.Id);
                    return(Ok(testAttendee));
                }
            }
        }