public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("Received recent shift request."); var claims = await _authenticationHelper.DecodeToken(req.Headers["Authorization"]); if (claims == null || !claims.Identity.IsAuthenticated) { log.LogInformation("Unauthorised request received."); return(new UnauthorizedResult()); } var shifts = await _shiftService.GetAllShifts(claims.Identity.Name); shifts = shifts.Where(s => s.Date.Date <= DateTime.Today) .OrderByDescending(s => s.Date).Take(6); return(new OkObjectResult(shifts.Select(s => new ShiftSummary { CrewMate = s.CrewMate, Date = s.Date, Duration = s.Duration, Event = s.Event, Id = s.Id, Location = s.Location, LoggedCalls = s.Jobs?.Count ?? 0, Role = s.Role }))); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("Received update shift request."); var claims = await _authenticationHelper.DecodeToken(req.Headers["Authorization"]); if (claims == null || !claims.Identity.IsAuthenticated) { log.LogInformation("Unauthorised request received."); return(new UnauthorizedResult()); } UpdatedShift updatedShift; try { updatedShift = JsonConvert.DeserializeObject <UpdatedShift>(await req.ReadAsStringAsync()); } catch (JsonException) { return(new BadRequestResult()); } var val = new UpdatedShiftValidator(); var res = await val.ValidateAsync(updatedShift); if (!res.IsValid) { log.LogInformation("Invalid request received."); return(new BadRequestResult()); } var previousShift = await _shiftService.GetShift(claims.Identity.Name, updatedShift.Id); if (previousShift == null) { return(new NotFoundResult()); } previousShift.CrewMate = updatedShift.CrewMate; previousShift.Date = updatedShift.Date; previousShift.Duration = updatedShift.Duration; previousShift.Event = updatedShift.Event; previousShift.Location = updatedShift.Location; previousShift.Role = updatedShift.Role; await _shiftService.UpdateShift(claims.Identity.Name, previousShift); return(new OkResult()); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("Received all shift request."); var claims = await _authenticationHelper.DecodeToken(req.Headers["Authorization"]); if (claims == null || !claims.Identity.IsAuthenticated) { log.LogInformation("Unauthorised request received."); return(new UnauthorizedResult()); } var countValid = int.TryParse(req.Query["count"], out var count); if (!countValid) { count = 10; } var pageValid = int.TryParse(req.Query["page"], out var page); if (!pageValid) { page = 0; } var shifts = await _shiftService.GetAllShifts(claims.Identity.Name); req.HttpContext.Response.Headers.Add("X-Total-Count", shifts.Count().ToString()); shifts = shifts.OrderByDescending(s => s.Date).Skip(page * count).Take(count); return(new OkObjectResult(shifts.Select(s => new ShiftSummary { CrewMate = s.CrewMate, Date = s.Date, Duration = s.Duration, Event = s.Event, Id = s.Id, Location = s.Location, LoggedCalls = s.Jobs?.Count ?? 0, Role = s.Role }))); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("Received log job request."); var claims = await _authenticationHelper.DecodeToken(req.Headers["Authorization"]); if (claims == null || !claims.Identity.IsAuthenticated) { log.LogInformation("Unauthorised request received."); return(new UnauthorizedResult()); } NewJob newJob; try { newJob = JsonConvert.DeserializeObject <NewJob>(await req.ReadAsStringAsync()); } catch (JsonException) { return(new BadRequestResult()); } var val = new NewJobValidator(); var res = await val.ValidateAsync(newJob); if (!res.IsValid) { log.LogInformation("Invalid request received."); return(new BadRequestResult()); } var storeRes = await _shiftService.AddJob(claims.Identity.Name, newJob); if (storeRes) { return(new OkResult()); } return(new BadRequestResult()); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("Received recent shift request."); var claims = await _authenticationHelper.DecodeToken(req.Headers["Authorization"]); if (claims == null || !claims.Identity.IsAuthenticated) { log.LogInformation("Unauthorised request received."); return(new UnauthorizedResult()); } var query = req.Query; if (!query.Keys.OfType <string>().Contains("id")) { return(new BadRequestResult()); } var id = query["id"]; var shift = await _shiftService.GetShift(claims.Identity.Name, id); if (shift == null) { return(new NotFoundResult()); } return(new OkObjectResult(new UpdatedShift { CrewMate = shift.CrewMate, Date = shift.Date, Duration = shift.Duration, Event = shift.Event, Id = shift.Id, Location = shift.Location, Role = shift.Role })); }
public async Task <IActionResult> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log) { log.LogInformation("Received shift job request."); var claims = await _authenticationHelper.DecodeToken(req.Headers["Authorization"]); if (claims == null || !claims.Identity.IsAuthenticated) { log.LogInformation("Unauthorised request received."); return(new UnauthorizedResult()); } var query = req.Query; if (!query.Keys.OfType <string>().Contains("shiftId")) { return(new BadRequestResult()); } var shiftId = query["shiftId"]; var shift = await _shiftService.GetShift(claims.Identity.Name, shiftId); if (shift == null) { return(new NotFoundResult()); } return(new OkObjectResult(shift.Jobs.Select(j => new JobSummary { Age = j.Age, Category = j.Category, Gender = j.Gender, ReflectionFlag = j.ReflectionFlag }))); }