Пример #1
0
        public async Task <ActionResult <JobsiteDto> > GetJobsiteWithTimestamps(string moniker, [FromQuery] TimestampParameters timestampParameters)
        {
            try
            {
                //manager status
                var loggedInUser = await _userRepository.GetUser(_userAccessor.GetCurrentUsername());

                if (loggedInUser.Manager == false)
                {
                    return(Unauthorized(new RestError(HttpStatusCode.Unauthorized, new { Unauthorized = "Unauthorized to perform action" })));
                }

                var jobsite = await _repository.GetJobsiteAsync(moniker);

                if (jobsite == null)
                {
                    return(NotFound($"Could not find jobsite with moniker of {moniker}"));
                }

                //get the timestamps for the jobsite in paged format
                var pagedTimestamps = await _timestampRepository.GetTimestampsForJobByDate(jobsite, timestampParameters);

                jobsite.Timestamps = pagedTimestamps;

                //Create metadata based on PagedList pagination
                var metadata = new
                {
                    pagedTimestamps.TotalCount,
                    pagedTimestamps.PageSize,
                    pagedTimestamps.CurrentPage,
                    pagedTimestamps.HasNext,
                    pagedTimestamps.HasPrevious
                };

                //Add metadata to header
                Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(metadata));

                var jobsiteDto = _mapper.Map <JobsiteWithTimestampsDto>(jobsite);

                //get all employees who visited this jobsite
                var jobsiteTimestamps = await _timestampRepository.GetTimestampsForJob(jobsite);

                var employees = TimestampActions.GetEmployeesFromJobsite(jobsiteTimestamps);
                jobsiteDto.EmployeesThatClocked = employees;

                return(Ok(jobsiteDto));
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Server Error: Failed to retrieve jobsite."));
            }
        }