public async Task <IActionResult> InviteParticipant(int id)
        {
            var participant = _dataSource.Participants.Get(id);             // TODO: Only a user with admin can do this.

            if (!String.IsNullOrWhiteSpace(participant.Email))
            {
                var message = _mailClient.CreateInvitation(participant);
                await _mailClient.SendAsync(message);

                return(Ok(true));
            }
            else
            {
                return(BadRequest(new JsonError(System.Net.HttpStatusCode.BadRequest, $"The participant '{participant.DisplayName}' does not have an email address.")));
            }
        }
Ejemplo n.º 2
0
        public IActionResult InviteParticipants(int calendarId)
        {
            var participants = _dataSource.Participants.GetForCalendar(calendarId);
            var errors       = new List <Exception>();

            foreach (var participant in participants)
            {
                if (!String.IsNullOrWhiteSpace(participant.Email))
                {
                    var t = Task.Run(async delegate
                    {
                        await Task.Delay(1000);

                        try
                        {
                            var message = _mailClient.CreateInvitation(participant);
                            await _mailClient.SendAsync(message);
                        }
                        catch (Exception ex)
                        {
                            // TODO: Handle and log errors differently.
                            errors.Add(new Exception($"Mail failed for {participant.DisplayName} - {participant.Email}", ex));
                        }
                    });

                    t.Wait();
                }
            }

            if (errors.Count() == 0)
            {
                return(new JsonResult(true));
            }

            // TODO: Don't include certain information in error message.
            return(new JsonResult(errors.Select(e => new { e.Message, InnerException = e.InnerException.Message })));
        }