示例#1
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()));
                }
            }
        }