public async Task <HttpResponseMessage> Post(HttpRequestMessage req, TraceWriter log) { if ((await _userDigestService.CurrentUserAsync(req)).AppRole == Role.Driver) { return(req.CreateResponse(HttpStatusCode.Forbidden)); } var payload = await req.Content.ReadAsAsync <Driver>(); var item = await _DriverService.CreateAsync(payload, req); return(req.CreateResponse(HttpStatusCode.Created, item)); }
public async Task <HttpResponseMessage> Get(HttpRequestMessage req, TraceWriter log) { var drivers = await _driverRepository.GetItemsAsync(t => t.deleted == false); var orgs = await _organisationRepository.GetItemsAsync(o => o.deleted == false); var userDigest = await _userDigestService.CurrentUserAsync(req); Organisation org = null; if (userDigest.AppRole == Role.Driver || userDigest.AppRole == Role.OrgAdmin) { org = await _organisationRepository.GetItemAsync(userDigest.OrganisationId); } var overview = new AppAdminDashboardOverview { driversNotExpiring = drivers.Count(), driversTotal = drivers.Count(), orgsNotExpiring = orgs.Count(), orgsTotal = orgs.Count(), orgId = userDigest.OrganisationId, orgName = org?.name, orgPhotoId = org?.PhotoId }; return(req.CreateResponse(HttpStatusCode.OK, overview)); }
public async Task <HttpResponseMessage> Delete(HttpRequestMessage req, TraceWriter log) { string id = req.GetQueryNameValuePairs().Where(w => w.Key == "id").FirstOrDefault().Value; if ((await _userDigestService.CurrentUserAsync(req)).AppRole == Role.Driver) { return(req.CreateResponse(HttpStatusCode.Forbidden)); } if (string.IsNullOrWhiteSpace(id)) { return(req.CreateResponse(HttpStatusCode.NotFound)); } var Journey = await _JourneyRepository.GetItemAsync(id); if (Journey == null) { return(req.CreateResponse(HttpStatusCode.NotFound)); } Journey = await _JourneyService.DeleteAsync(Journey, req); return(req.CreateResponse(HttpStatusCode.OK, Journey)); }
public async Task <T> CreateItemAsync(T item, HttpRequestMessage req) { //TODO: Should be trigger or stored proc. var digest = await _userDigestService.CurrentUserAsync(req); var now = DateTime.Now; item.changedById = digest.Id; item.changedByName = digest.DisplayName; item.changedOn = now; var document = await _documentClient.CreateDocumentAsync(CollectionUri, item); var changeRecord = await AddChangeRecord($"Added {item.name}", digest, "Created", null, item, now); var newItem = (T)(dynamic)document.Resource; return(newItem); }
public async Task <HttpResponseMessage> Get(HttpRequestMessage req, TraceWriter log) { try { var organisationId = req.GetQueryNameValuePairs().Where(w => w.Key == "organisationId").FirstOrDefault().Value; bool isAdmin = await _userDigestService.IsAdminAsync(req); var userDigest = await _userDigestService.CurrentUserAsync(req); if (!string.IsNullOrEmpty(organisationId) && (isAdmin || userDigest.OrganisationId == organisationId)) { var orgUsers = await _userService.GetByOrgAsync(organisationId); return(req.CreateResponse(HttpStatusCode.OK, orgUsers.ToList())); } var userId = req.GetQueryNameValuePairs().Where(w => w.Key == "userId").FirstOrDefault().Value; var allUsers = new List <UserDB>(); if (!string.IsNullOrEmpty(userId)) { var singleUser = await _userService.GetUserAsync(userId, req); allUsers.Add(singleUser); } else { var searchTerm = req.GetQueryNameValuePairs().Where(w => w.Key == "SearchTerm").FirstOrDefault().Value; if (!string.IsNullOrEmpty(searchTerm)) { allUsers.AddRange(await _userService.SearchAsync(searchTerm, req)); } else { var users = await _userService.GetAllUsers(req); allUsers.AddRange(users); } } return(req.CreateResponse(HttpStatusCode.OK, allUsers)); } catch (Exception exp) { log.Error("Error retrieving users: " + exp.Message + exp.StackTrace); return(req.CreateErrorResponse(HttpStatusCode.InternalServerError, exp)); } }
public async Task <SearchResults <U> > Search <T, U>(HttpRequestMessage req, TraceWriter log) where T : BaseModel where U : class { var token = req.GetQueryNameValuePairs().Where(w => w.Key == "token").FirstOrDefault().Value; var sort = req.GetQueryNameValuePairs().Where(w => w.Key == "sort").FirstOrDefault().Value; var sortAscending = req.GetQueryNameValuePairs().Where(w => w.Key == "sortAscending").FirstOrDefault().Value; var pageSize = req.GetQueryNameValuePairs().Where(w => w.Key == "pageSize").FirstOrDefault().Value; var filterNames = req.GetQueryNameValuePairs().Where(w => w.Key == "f").Select(q => q.Value).ToList(); var values = req.GetQueryNameValuePairs().Where(w => w.Key == "v").Select(q => q.Value).ToList(); var filters = new List <GridQueryFilter>(); for (int i = 0; i < filterNames.Count; i++) { filters.Add(new GridQueryFilter() { column = filterNames[i], value = values.ElementAtOrDefault(i) }); } var gridQuery = new GridQuery() { filters = filters, token = token, sort = sort, }; int page; if (string.IsNullOrWhiteSpace(pageSize) == false && int.TryParse(pageSize, out page)) { gridQuery.pageSize = page; } bool sortAsc = false; bool.TryParse(sortAscending, out sortAsc); gridQuery.sortAscending = sortAsc; var container = ContainerRegistrar.GetContainer(); var queryBuilder = container.GetInstance <IQueryBuilder <T, U> >(); var userDigest = await _userDigestService.CurrentUserAsync(req); var searchResults = await _searchService.Search <T, U>(queryBuilder, token, gridQuery, userDigest); return(searchResults); }
public async Task <HttpResponseMessage> Get(HttpRequestMessage req, TraceWriter log) { string vehicleId = req.GetQueryNameValuePairs().Where(w => w.Key == "id").FirstOrDefault().Value; if ((await _userDigestService.CurrentUserAsync(req)).AppRole == Role.Driver) { return(req.CreateResponse(HttpStatusCode.Forbidden)); } if (string.IsNullOrWhiteSpace(vehicleId)) { var searchResults = await _searchApi.Search <Vehicle, Vehicle>(req, log); bool replaceUnapproved = false; bool.TryParse(req.GetQueryNameValuePairs().Where(w => w.Key == "replaceUnapproved").FirstOrDefault().Value, out replaceUnapproved); return(req.CreateResponse(HttpStatusCode.OK, searchResults)); } var org = await _vehicleService.GetAsync(vehicleId); return(req.CreateResponse(HttpStatusCode.OK, org)); }