Exemplo n.º 1
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
        {
            _logger.LogInformation($"C# HTTP trigger function AddCommentToCalendarItem processed a request.");
            string tenant = req.Headers[Constants.HEADER_TENANT];

            if (String.IsNullOrWhiteSpace(tenant))
            {
                tenant = null;
            }
            ServerSettings serverSettings = await _serverSettingsRepository.GetServerSettings(tenant);

            string keyWord = req.Headers[Constants.HEADER_KEYWORD];

            if (String.IsNullOrEmpty(keyWord) || !serverSettings.IsUser(keyWord))
            {
                return(new BadRequestErrorMessageResult("Keyword is missing or wrong."));
            }
            string          requestBody = await new StreamReader(req.Body).ReadToEndAsync();
            CalendarComment comment     = JsonConvert.DeserializeObject <CalendarComment>(requestBody);

            // Get and check corresponding CalendarItem
            if (String.IsNullOrEmpty(comment.CalendarItemId))
            {
                return(new OkObjectResult(new BackendResult(false, "Terminangabe fehlt.")));
            }
            CalendarItem calendarItem = await _calendarRepository.GetItem(comment.CalendarItemId);

            if (null == calendarItem)
            {
                return(new OkObjectResult(new BackendResult(false, "Angegebenen Termin nicht gefunden.")));
            }
            ExtendedCalendarItem extendedCalendarItem = new ExtendedCalendarItem(calendarItem);

            // Read all participants for this calendar item
            extendedCalendarItem.ParticipantsList = await _participantRepository.GetItems(p => p.CalendarItemId.Equals(extendedCalendarItem.Id));

            // Set TTL for comment the same as for CalendarItem
            System.TimeSpan diffTime = calendarItem.StartDate.Subtract(DateTime.Now);
            comment.TimeToLive = serverSettings.AutoDeleteAfterDays * 24 * 3600 + (int)diffTime.TotalSeconds;
            // Checkindate to track bookings
            comment.CommentDate = DateTime.Now;
            if (!String.IsNullOrWhiteSpace(tenant))
            {
                comment.Tenant = tenant;
            }

            comment = await _cosmosRepository.UpsertItem(comment);

            if (!String.IsNullOrEmpty(comment.Comment))
            {
                await _subscriptionRepository.NotifyParticipants(extendedCalendarItem, comment.AuthorFirstName, comment.AuthorLastName, comment.Comment);
            }

            BackendResult result = new BackendResult(true);

            return(new OkObjectResult(result));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
        {
            _logger.LogInformation("C# HTTP trigger function WriteCalendarItem processed a request.");
            string tenant = req.Headers[Constants.HEADER_TENANT];

            if (String.IsNullOrWhiteSpace(tenant))
            {
                tenant = null;
            }
            ServerSettings serverSettings;

            if (null == tenant)
            {
                serverSettings = await _serverSettingsRepository.GetServerSettings();
            }
            else
            {
                serverSettings = await _serverSettingsRepository.GetServerSettings(tenant);
            }

            string keyWord = req.Headers[Constants.HEADER_KEYWORD];

            if (String.IsNullOrEmpty(keyWord) || !serverSettings.IsUser(keyWord))
            {
                return(new BadRequestErrorMessageResult("Keyword is missing or wrong."));
            }
            string       requestBody  = await new StreamReader(req.Body).ReadToEndAsync();
            CalendarItem calendarItem = JsonConvert.DeserializeObject <CalendarItem>(requestBody);

            System.TimeSpan diffTime = calendarItem.StartDate.Subtract(DateTime.Now);
            calendarItem.TimeToLive = serverSettings.AutoDeleteAfterDays * 24 * 3600 + (int)diffTime.TotalSeconds;
            if (null != tenant)
            {
                calendarItem.Tenant = tenant;
            }
            CalendarItem oldCalendarItem = null;

            if (!String.IsNullOrEmpty(calendarItem.Id))
            {
                oldCalendarItem = await _cosmosRepository.GetItem(calendarItem.Id);
            }
            calendarItem = await _cosmosRepository.UpsertItem(calendarItem);

            if (null != oldCalendarItem)
            {
                string message = null;
                // Compare versions and generate message
                if (oldCalendarItem.IsCanceled != calendarItem.IsCanceled)
                {
                    if (calendarItem.IsCanceled)
                    {
                        message = "Abgesagt!";
                    }
                    else
                    {
                        message = "Absage rückgängig gemacht!";
                    }
                }
                else if (!oldCalendarItem.StartDate.Equals(calendarItem.StartDate))
                {
                    message = "Neue Startzeit!";
                }
                else if (!oldCalendarItem.Title.Equals(calendarItem.Title))
                {
                    message = "Titel geändert;";
                }
                else if (!oldCalendarItem.Place.Equals(calendarItem.Place))
                {
                    message = "Startort geändert!";
                }
                else if (!oldCalendarItem.Summary.Equals(calendarItem.Summary))
                {
                    message = "Neue Infos!";
                }
                if (null != message)
                {
                    ExtendedCalendarItem extendedCalendarItem = new ExtendedCalendarItem(calendarItem);
                    // Read all participants for this calendar item
                    extendedCalendarItem.ParticipantsList = await _participantRepository.GetItems(p => p.CalendarItemId.Equals(extendedCalendarItem.Id));

                    await _subscriptionRepository.NotifyParticipants(extendedCalendarItem, extendedCalendarItem.HostFirstName, extendedCalendarItem.HostLastName, message);
                }
            }

            return(new OkObjectResult(calendarItem));
        }