public async Task <IActionResult> Post( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "entries")] [RequestBodyType(typeof(CreateEntryRequest), "CreateEntryRequest")] HttpRequest req, ILogger log) { string authenticatedEmail; try { authenticatedEmail = _tokenService.GetEmailFromBearerToken(req); } catch (JournallyException ex) { log.LogWarning($"Authorization error when calling /entries: {ex.Message}"); return(new UnauthorizedResult()); } if (req.ContentLength <= 0) { return(HttpCodeHelper.EmptyRequestBody()); } string requestBody; using (StreamReader readStream = new StreamReader(req.Body)) { requestBody = await readStream.ReadToEndAsync(); } Entry data; try { data = JsonConvert.DeserializeObject <Entry>(requestBody); } catch (Exception e) { return(HttpCodeHelper.Return400(e.Message)); } if (data.DateTime == null) { return(HttpCodeHelper.Return400("Date provided cannot be null.")); } if (data.Content == null) { return(HttpCodeHelper.Return400("Content provided cannot be null.")); } var user = await _userService.GetByEmailAsync(authenticatedEmail); data.UserId = user.Id; await _entryService.AddEntryAsync(data); return(new CreatedResult("https://example.com/api/entries/201", data)); }
public async Task <IActionResult> Authenticate( [HttpTrigger( AuthorizationLevel.Anonymous, "post", Route = "user/authenticate")] [RequestBodyType(typeof(AuthenticateUserRequest), "Authenticate User Request")] HttpRequest req, ILogger log) { if (req.ContentLength <= 0) { return(HttpCodeHelper.EmptyRequestBody()); } string requestBody; using (StreamReader readStream = new StreamReader(req.Body)) { requestBody = await readStream.ReadToEndAsync(); } RegisterUserRequest payload; try { payload = JsonConvert.DeserializeObject <RegisterUserRequest>(requestBody); } catch (Exception e) { return(HttpCodeHelper.Return400(e.Message)); } User user; try { user = _userService.Authenticate(payload.Email, payload.Password); } catch (JournallyException ex) { return(HttpCodeHelper.Return400(ex.Message)); } var tokenString = _tokenService.GenerateToken(user); var response = new AuthenticateUserResponse { Email = user.Email, Token = tokenString }; return(new OkObjectResult(response)); }
public async Task <IActionResult> Register( [HttpTrigger( AuthorizationLevel.Anonymous, "post", Route = "user")] [RequestBodyType(typeof(RegisterUserRequest), "Register User Request")] HttpRequest req) { if (req.ContentLength <= 0) { return(HttpCodeHelper.EmptyRequestBody()); } string requestBody; using (StreamReader readStream = new StreamReader(req.Body)) { requestBody = await readStream.ReadToEndAsync(); } RegisterUserRequest payload; try { payload = JsonConvert.DeserializeObject <RegisterUserRequest>(requestBody); } catch (Exception e) { return(HttpCodeHelper.Return400(e.Message)); } var newUser = new User { Email = payload.Email }; try { _userService.Create(newUser, payload.Password); } catch (JournallyException ex) { return(HttpCodeHelper.Return400(ex.Message)); } var response = new RegisterUserResponse { Email = newUser.Email }; return(new CreatedResult("https://example.com/api/entries/201", response)); }
public async Task <IActionResult> Put( [HttpTrigger(AuthorizationLevel.Anonymous, "put", Route = "entries/{entryId}")] [RequestBodyType(typeof(UpdateEntryRequest), "UpdateEntryRequest")] HttpRequest req, ILogger log, int entryId) { string authenticatedEmail; try { authenticatedEmail = _tokenService.GetEmailFromBearerToken(req); } catch (JournallyException ex) { log.LogWarning($"Authorization error when calling /entries: {ex.Message}"); return(new UnauthorizedResult()); } if (req.ContentLength <= 0) { return(HttpCodeHelper.EmptyRequestBody()); } User user = await _userService.GetByEmailAsync(authenticatedEmail); Entry entry = await _entryService.GetEntryByIdAsync(user, entryId); if (entry == null) { return(new NotFoundResult()); } string requestBody; using (StreamReader readStream = new StreamReader(req.Body)) { requestBody = await readStream.ReadToEndAsync(); } Entry data; try { data = JsonConvert.DeserializeObject <Entry>(requestBody); } catch (Exception e) { return(HttpCodeHelper.Return400(e.Message)); } if (data.DateTime == null) { return(HttpCodeHelper.Return400("Date provided cannot be null.")); } if (data.Content == null) { return(HttpCodeHelper.Return400("Content provided cannot be null.")); } await _entryService.UpdateEntryAsync(entryId, data); return(new NoContentResult()); }