Esempio n. 1
0
        public async Task <IActionResult> DeleteJobsite(string moniker)
        {
            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" })));
                }

                //find jobsite
                var jobsite = await _repository.GetJobsiteAsync(moniker);

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

                //confirm nobody is currently clocked in at jobsite
                var anyoneClockedIn = await _timestampRepository.JobsiteHasClockedInTimestamp(jobsite);

                if (anyoneClockedIn)
                {
                    return(BadRequest($"Cannot delete jobsite {moniker} - There are users clocked in."));
                }

                _repository.Delete(jobsite);

                if (await _repository.SaveChangesAsync())
                {
                    return(Ok("Jobsite deleted."));
                }
            }
            catch (Exception)
            {
                return(this.StatusCode(StatusCodes.Status500InternalServerError, "Server Error: Failed to delete jobsite."));
            }
            return(BadRequest());
        }