Example #1
0
        public async Task <IActionResult> GetALocation([FromRoute] int locationId)
        {
            try
            {
                var location = await locationsRepository.GetALocation(locationId);

                if (location == null)
                {
                    var error = new NotFoundException($"No location at city '{locationId}' found");
                    return(StatusCode(StatusCodes.Status404NotFound, new CustomException <NotFoundException>(error).GetException()));
                }
                var resource = mapper.Map <Location, LocationResource>(location);
                var response = new OkResponse <LocationResource>(resource, "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()));
                }
            }
        }
        public async Task <ActionResult <Location> > GetALocation(string locationCode)
        {
            var response = await locationsRepository.GetALocation(locationCode);

            var viewModel = mapper.Map <Location>(response);

            return(Ok(viewModel));
        }
Example #3
0
        public async Task <IActionResult> CreateAProject([FromBody] ProjectProfile projectProfile)
        {
            if (projectProfile == null)
            {
                var error = new BadRequestException("The given project is null / Request Body cannot be read");
                return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
            }

            if (
                projectProfile.ProjectManager == null || String.IsNullOrEmpty(projectProfile.ProjectManager.UserID) ||
                projectProfile.ProjectSummary == null ||
                String.IsNullOrEmpty(projectProfile.ProjectSummary.ProjectNumber) ||
                projectProfile.ProjectSummary.Location == null
                )
            {
                var error = new BadRequestException("The Project (Manager(ID) / Summary / Number / Location) cannot be null or empty string!");
                return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
            }
            try
            {
                var location = await locationsRepository.GetALocation(projectProfile.ProjectSummary.Location.City);

                var createdProjectNumber = await projectsRepository.CreateAProject(projectProfile, location.Id);

                var response = new CreatedResponse <string>(createdProjectNumber, $"Successfully created project number '{createdProjectNumber}'");
                return(StatusCode(StatusCodes.Status201Created, 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 #4
0
        public async Task <IActionResult> UpdateUser([FromBody] UserProfile userProfile, string userId)
        {
            if (userProfile == null)
            {
                var error = new BadRequestException("The given user is null / Request Body cannot be read");
                return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
            }

            if (
                userProfile.UserSummary == null || String.IsNullOrEmpty(userProfile.UserSummary.UserID) ||
                userProfile.UserSummary.Location == null ||
                userProfile.Availability == null || userProfile.Disciplines == null
                )
            {
                var error = new BadRequestException("The User (Summary(ID) / Location / Availability / Disciplines) cannot be null or empty string!");
                return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
            }

            if (!String.Equals(userProfile.UserSummary.UserID, userId))
            {
                var errMessage = $"The user ID on URL '{userId}'" +
                                 $" does not match with '{userProfile.UserSummary.UserID}' in Request Body's Project Summary";
                var error = new BadRequestException(errMessage);
                return(StatusCode(StatusCodes.Status400BadRequest, new CustomException <BadRequestException>(error).GetException()));
            }

            try
            {
                UserSummary summary  = userProfile.UserSummary;
                Location    location = await locationsRepository.GetALocation(userProfile.UserSummary.Location.City);

                var changedUser = await usersRepository.UpdateAUser(summary, location);

                if (changedUser == "-1")
                {
                    var error = new InternalServerException($"Cannot Update User with id: {userId}");
                    return(StatusCode(StatusCodes.Status500InternalServerError, new CustomException <InternalServerException>(error).GetException()));
                }
                var disciplines = await processDisciplineSkillChanges(userProfile.Disciplines, userId);

                var avails = await processOutOfOfficeChanges(userProfile.Availability, userId);

                var tmp      = new { changedUser, disciplines, avails };
                var response = new OkResponse <string>(userId, "Successfully updated");
                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()));
                }
            }
        }