Exemplo n.º 1
0
 public async Task Run([IoTHubTrigger("messages/events", Connection = "IoTHubTriggerConnection")] EventData message, ILogger log)
 {
     var requestBody = Encoding.UTF8.GetString(message.Body.Array);
     var attendance  = JsonConvert.DeserializeObject <Attendance>(requestBody);
     await _attendanceRepository.Create(attendance.UserId, attendance);
 }
Exemplo n.º 2
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", "put", Route = "{userId}/attendance")]
            HttpRequest req,
            ILogger log, string userId)
        {
            if (IsNullOrEmpty(userId))
            {
                return(new BadRequestResult());
            }
            if (!req.isAuthorized(userId))
            {
                return(new UnauthorizedResult());
            }

            var requestBody = await new StreamReader(req.Body).ReadToEndAsync();

            log.LogInformation($"Raw request: {requestBody}");
            var attendance = JsonConvert.DeserializeObject <Attendance>(requestBody);

            if (attendance == null)
            {
                return(new BadRequestResult());
            }

            if (attendance?.OccurredAt == DateTimeOffset.MinValue)
            {
                attendance.OccurredAt = DateTimeOffset.UtcNow;
            }

            switch (req.Method)
            {
            case "POST":
            {
                var ok = await _attendanceRepository.Create(userId, attendance);

                if (!ok)
                {
                    return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
                }

                break;
            }

            case "PUT":
            {
                var ok = await _attendanceRepository.Update(userId, attendance);

                if (!ok)
                {
                    return(new StatusCodeResult(StatusCodes.Status500InternalServerError));
                }

                break;
            }

            default:
                return(new StatusCodeResult(StatusCodes.Status405MethodNotAllowed));
            }

            return(new StatusCodeResult(StatusCodes.Status200OK));
        }