Exemplo n.º 1
0
        public LoginDTO GetEmployeeLoginByUsernameAndPassword(string username, string password)
        {
            //Hash password first
            if (password == null)
            {
                return(null);
            }

            string hashedPassword = HashPassword(password);

            Employee e = employeeRepo.FindBy(x => x.Username == username && x.Password == hashedPassword).SingleOrDefault();

            if (e == null) // no such user
            {
                return(null);
            }
            else
            {
                Session newSession = new Session()
                {
                    EmployeeId = e.Id, GUID = Guid.NewGuid().ToString(), LogInDateTime = DateTime.Now
                };
                sessionRepo.Create(newSession);

                //check if cover head?
                int role = departmentCoverEmployeeRepo.FindOneBy(x => x.EmployeeId == e.Id && x.Status == "ACTIVE" && DbFunctions.TruncateTime(x.FromDate) <= DateTime.Now && DbFunctions.TruncateTime(x.ToDate) >= DateTime.Now) != null ? (int)Enums.Roles.DepartmentCoverHead : e.RoleId;

                //set attributes
                LoginDTO loginDTO = new LoginDTO()
                {
                    EmployeeId = e.Id, RoleId = role, SessionGuid = newSession.GUID, Department = e.Department.DepartmentName
                };
                return(loginDTO);
            }
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
    /// <summary>Uploads a List of CSVSessionUpload to the database</summary>
    public async Task <int> UploadSessions(IEnumerable <CSVSessionUpload> sessionUploads)
    {
        var errorList          = new List <string> ();
        var personExistsErrors = (await Task.WhenAll(sessionUploads.Select(async x => {
            var personExists = await personRepo.Exist(p => p.Email == x.Email);
            if (!personExists)
            {
                return($"Person with Email: '{x.Email}' does not exist");
            }
            return(null);
        }))).Where(x => x != null);

        var classExistsErrors = (await Task.WhenAll(sessionUploads.SelectMany(x => x.CRNs).Select(async x => {
            var classExists = await classRepo.Exist(c => c.CRN == x);
            if (!classExists)
            {
                return($"Class with CRN: {x} does not exist");
            }
            return(null);
        }))).Where(x => x != null);

        var reasonExistsErrors = (await Task.WhenAll(sessionUploads.SelectMany(x => x.Reasons).Select(async x => {
            var reasonExists = await reasonRepo.Exist(c => c.Name == x);
            if (!reasonExists)
            {
                return($"Reason with Name: '{x}' does not exist");
            }
            return(null);
        }))).Where(x => x != null);

        var semesterExistError = (await Task.WhenAll(sessionUploads.Select(x => x.SemesterCode).Select(async x => {
            var semesterExists = await semesterRepo.Exist(c => c.Code == x);
            if (!semesterExists)
            {
                return($"Semester with Code: '{x}' does not exist");
            }
            return(null);
        }))).Where(x => x != null);

        errorList.AddRange(personExistsErrors);
        errorList.AddRange(classExistsErrors);
        errorList.AddRange(reasonExistsErrors);
        errorList.AddRange(semesterExistError);

        if (errorList.Count() != 0)
        {
            throw new TCSException(String.Join("\n", errorList));
        }

        var sessionList = await Task.WhenAll(sessionUploads.Select(async x => new Session()
        {
            Tutoring       = x.Tutoring,
            InTime         = x.InTime,
            OutTime        = x.OutTime,
            PersonId       = (await personRepo.Find(p => p.Email == x.Email)).Id,
            SemesterCode   = x.SemesterCode,
            SessionClasses = x.CRNs
                             .Select(c => new SessionClass()
            {
                ClassId = c
            }).ToList(),
            SessionReasons = await Task.WhenAll(x.Reasons
                                                .Select(s => s.Trim())
                                                .Select(async r => {
                var reason = await reasonRepo.Find(re => re.Name == r);
                return(new SessionReason()
                {
                    ReasonId = reason.Id
                });
            }))
        }));

        return(await sessionRepo.Create(sessionList));
    }