Example #1
0
        public async Task <IActionResult> UnassignResource([FromRoute] int openingId)
        {
            if (openingId == 0)
            {
                var error = new BadRequestException($"The given opening {openingId} is invalid");
                return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
            }
            try
            {
                Position position = await positionsRepository.GetAPosition(openingId);

                if (position.ResourceId == null)
                {
                    var error = new BadRequestException($"Position does not have a resource to unassign");
                    return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
                }

                User user = await usersRepository.GetAUser(position.ResourceId);

                if (position == null)
                {
                    var error = new NotFoundException($"Invalid positionId {openingId}.");
                    return(StatusCode(StatusCodes.Status404NotFound, new CustomException <NotFoundException>(error).GetException()));
                }
                if (user == null)
                {
                    var error = new NotFoundException($"Resource with id {position.ResourceId} not found.");
                    return(StatusCode(StatusCodes.Status404NotFound, new CustomException <NotFoundException>(error).GetException()));
                }
                position.ResourceId  = null;
                position.IsConfirmed = false;

                position = await positionsRepository.UpdateAPosition(position);

                IEnumerable <Position> positions = await positionsRepository.GetAllPositionsOfUser(user.Id);

                IEnumerable <OutOfOffice> outOfOffices = await outOfOfficeRepository.GetAllOutOfOfficeForUser(user.Id);

                int newUtilizationOfUser = await utilizationRepository.CalculateUtilizationOfUser(positions, outOfOffices);

                user = await usersRepository.UpdateUtilizationOfUser(newUtilizationOfUser, user.Id);

                OpeningPositionsResource openingRes = await positionsRepository.GetAnOpeningPositionsResource(openingId);

                OpeningPositionsSummary openingSummary = mapper.Map <OpeningPositionsResource, OpeningPositionsSummary>(openingRes);
                RequestUnassign         response       = new RequestUnassign {
                    OpeningId            = position.Id,
                    UserId               = user.Id,
                    ConfirmedUtilization = user.Utilization,
                    Opening              = openingSummary
                };


                return(StatusCode(StatusCodes.Status200OK, response));
            }
            catch (Exception err)
            {
                var errMessage = $"Source: {err.Source}\n  Message: {err.Message}\n  StackTrace: {err.StackTrace}\n";
                if (err is SqlException)
                {
                    var error = new InternalServerException(errMessage);
                    return(StatusCode(StatusCodes.Status500InternalServerError, new CustomException <InternalServerException>(error).GetException()));
                }
                else
                {
                    var error = new BadRequestException(errMessage);
                    return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
                }
            }
        }
Example #2
0
        public async Task <IActionResult> GetAProject(string projectNumber)
        {
            if (String.IsNullOrEmpty(projectNumber))
            {
                var error = new BadRequestException("The given project number is null/empty");
                return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
            }
            try
            {
                var project = await projectsRepository.GetAProjectResource(projectNumber);

                if (project == null)
                {
                    var error = new NotFoundException($"No project at projectNumber '{projectNumber}' found");
                    return(StatusCode(StatusCodes.Status404NotFound, new CustomException <NotFoundException>(error).GetException()));
                }
                var projectSummary = mapper.Map <ProjectResource, ProjectSummary>(project);

                var projectManager = mapper.Map <ProjectResource, ProjectManager>(project);

                var users = await usersRepository.GetAllUserResourcesOnProject(project.Id, project.ManagerId);

                if (users == null || !users.Any())
                {
                    users = new UserResource[] { };
                }
                var usersSummary = mapper.Map <IEnumerable <UserResource>, IEnumerable <UserSummary> >(users);

                var openingPositions = await positionsRepository.GetAllUnassignedPositionsResourceOfProject(project.Id);

                if (openingPositions == null || !openingPositions.Any())
                {
                    openingPositions = new OpeningPositionsResource[] { };
                }
                var openingPositionsSummary = mapper.Map <IEnumerable <OpeningPositionsResource>, IEnumerable <OpeningPositionsSummary> >(openingPositions);

                var projectProfile = new ProjectProfile
                {
                    ProjectSummary = projectSummary,
                    ProjectManager = projectManager,
                    UsersSummary   = usersSummary,
                    Openings       = openingPositionsSummary
                };

                var response = new OkResponse <ProjectProfile>(projectProfile, "Everything is good");
                return(StatusCode(StatusCodes.Status200OK, response));
            }
            catch (Exception err)
            {
                var errMessage = $"Source: {err.Source}\n  Message: {err.Message}\n  StackTrace: {err.StackTrace}\n";
                if (err is SqlException)
                {
                    var error = new InternalServerException(errMessage);
                    return(StatusCode(StatusCodes.Status500InternalServerError, new CustomException <InternalServerException>(error).GetException()));
                }
                else
                {
                    var error = new BadRequestException(errMessage);
                    return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
                }
            }
        }