Пример #1
0
        public async Task <IActionResult> CreateSchedule(
            Json.Schedule schedule)
        {
            try
            {
                var claim = new S.PrincipalClaim(
                    Guid.Parse(GetSubjectId),
                    schedule.Name
                    );

                var r = await _repo.Add(claim);

                if (r == S.CreateScheduleResult.Created)
                {
                    _log.LogInformation(
                        LoggingEvents.ScheduleCreated,
                        "Schedule created with claim {claim}",
                        claim
                        );

                    return(Created(
                               $"api/schedule/{schedule.Name}",
                               new Json.Result
                    {
                        Success = true,
                        Links = new []
                        {
                            new Json.Link
                            {
                                Href = $"api/schedule/{schedule.Name}",
                                Ref = "self",
                                Type = "GET"
                            }
                        }
                    }));
                }
                else
                {
                    return(Conflict(new Json.Result
                    {
                        Success = false,
                        Message = "Already created.",
                        Links = new []
                        {
                            new Json.Link
                            {
                                Href = $"api/schedule/{schedule.Name}",
                                Ref = "self",
                                Type = "GET"
                            }
                        }
                    }));
                }
            }
            catch (FormatException fmt)
            {
                _log.LogWarning($"Malformed sub-claim.", fmt);
                return(BadRequest());
            }
        }
Пример #2
0
        public static TestCase <Data.Schedule, S.PrincipalClaim, S.Appointment[]> GetSchedule(string s)
        {
            switch (s)
            {
            case "NotOnRecord":
                return(new TestCase <Data.Schedule, S.PrincipalClaim, S.Appointment[]>
                {
                    Given = new Data.Schedule[0],
                    Arguments = new S.PrincipalClaim(
                        Guid.NewGuid(),
                        Guid.NewGuid().ToString()
                        ),
                    Expect = new S.Appointment[0]
                });

            case "OnRecord":
                var claim = new S.PrincipalClaim(
                    Guid.NewGuid(),
                    Guid.NewGuid().ToString()
                    );

                var seed = Arbitrary.Appointments(
                    DateTimeOffset.Now.ToUnixTimeSeconds(),
                    1800,
                    2,
                    claim.Schedule
                    ).Take(10).ToList();

                return(new TestCase <Data.Schedule, S.PrincipalClaim, S.Appointment[]>
                {
                    Given = new[]
                    {
                        new Data.Schedule
                        {
                            PrincipalId = claim.Id,
                            Name = claim.Schedule,
                            Appointments = seed
                        }
                    },
                    Arguments = claim,
                    Expect = seed
                             .Select(x => new S.Appointment
                    {
                        Start = x.Start,
                        Participants = x.Participants
                                       .Select(p => new S.Participation
                        {
                            SubjectId = p.SubjectId,
                            Name = p.Name
                        }).ToList()
                    }).OrderBy(x => x.Start).ToArray()
                });

            default:
                throw new ArgumentException(nameof(s));
            }
        }
Пример #3
0
        public async Task <D.DeleteAppointmentResult> DeleteAppointment(
            D.PrincipalClaim c,
            long start)
        {
            var r = new D.ClaimNotOnRecord() as D.DeleteAppointmentResult;

            var claim = await(from s in _ctx.Schedules
                              where s.PrincipalId == c.Id &&
                              s.Name == c.Schedule
                              select s)
                        .SingleOrDefaultAsync();

            if (claim != null)
            {
                var appt = await(from a in _ctx.Appointments
                                 where a.ScheduleName == c.Schedule &&
                                 a.Start == start
                                 select a)
                           .Include(x => x.Participants)
                           .SingleOrDefaultAsync();

                if (appt == null)
                {
                    r = new D.AppointmentNotOnRecord();
                }
                else
                {
                    _ctx.Remove(appt);
                    await _ctx.SaveChangesAsync();

                    r = new D.Deleted(new D.Appointment
                    {
                        Start        = appt.Start,
                        Duration     = appt.Duration,
                        Participants = appt.Participants
                                       .Select(x => new D.Participation
                        {
                            SubjectId = x.SubjectId,
                            Name      = x.Name
                        })
                                       .ToList()
                    });
                }
            }

            return(r);
        }
Пример #4
0
        public async Task <D.PostAppointmentResult> PostAppointment(
            D.PrincipalClaim c,
            D.Appointment ap)
        {
            var result = D.PostAppointmentResult.ClaimNotOnRecord;

            var claim = await(from s in _ctx.Schedules
                              where s.PrincipalId == c.Id &&
                              s.Name == c.Schedule
                              select s)
                        .SingleOrDefaultAsync();

            if (claim != null)
            {
                var appt = await(from a in _ctx.Appointments
                                 where a.ScheduleName == c.Schedule &&
                                 a.Start == ap.Start
                                 select a).AnyAsync();

                if (!appt)
                {
                    _ctx.Add(new Appointment
                    {
                        Schedule     = claim,
                        Start        = ap.Start,
                        Duration     = ap.Duration,
                        Participants = ap.Participants
                                       .Select(x => new Participant
                        {
                            SubjectId = x.SubjectId,
                            Name      = x.Name
                        }).ToList()
                    });

                    await _ctx.SaveChangesAsync();

                    result = D.PostAppointmentResult.Created;
                }
                else
                {
                    result = D.PostAppointmentResult.Conflict;
                }
            }

            return(result);
        }
Пример #5
0
        public async Task <D.Appointment[]> Get(
            D.PrincipalClaim c)
        {
            var q = from a in _ctx.Appointments
                    orderby a.Start
                    where a.Schedule.PrincipalId == c.Id &&
                    a.ScheduleName == c.Schedule
                    select new D.Appointment
            {
                Start        = a.Start,
                Duration     = a.Duration,
                Participants = a.Participants
                               .Select(x => new D.Participation
                {
                    SubjectId = x.SubjectId,
                    Name      = x.Name
                })
                               .ToList()
            };

            return(await q.ToArrayAsync());
        }
Пример #6
0
 public static TestCase <Data.Schedule, (S.PrincipalClaim, S.Appointment), S.PostAppointmentResult> PutAppointment(string s)
 {
     switch (s)
     {
     case "UnknownClaim":
         var claim3 = new S.PrincipalClaim(
             Guid.NewGuid(),
             Guid.NewGuid().ToString());
         var input2 = new S.Appointment
         {
             Start        = DateTimeOffset.Now.ToUnixTimeSeconds(),
             Participants = Arbitrary.Participants()
                            .Select(x => new S.Participation
             {
                 SubjectId = x.SubjectId,
                 Name      = x.Name
             }).Take(2).ToList()
         };
         return(new TestCase <Data.Schedule, (S.PrincipalClaim, S.Appointment), S.PostAppointmentResult>
         {
             Given = new Data.Schedule[0],
             Arguments = (claim3, input2),
             Expect = S.PostAppointmentResult.ClaimNotOnRecord
         });
Пример #7
0
        public async Task <D.CreateScheduleResult> Add(
            D.PrincipalClaim claim)
        {
            var added = await(
                from s in _ctx.Schedules
                where s.PrincipalId == claim.Id &&
                s.Name == claim.Schedule
                select s).AnyAsync();

            if (!added)
            {
                _ctx.Schedules.Add(new Schedule
                {
                    PrincipalId = claim.Id,
                    Name        = claim.Schedule
                });

                await _ctx.SaveChangesAsync();

                return(D.CreateScheduleResult.Created);
            }

            return(D.CreateScheduleResult.Conflict);
        }
Пример #8
0
        public static TestCase <Data.Schedule, HttpRequestMessage, HttpStatusCode> GetRequests(string key)
        {
            S.PrincipalClaim claim         = null;
            Data.Schedule    schedule      = null;
            Data.Appointment appointment   = null;
            string           participantId = null;
            string           scheduleName  = null;

            switch (key)
            {
            case "Known principal list Schedules":
                claim = new S.PrincipalClaim(
                    Guid.NewGuid(),
                    Guid.NewGuid().ToString()
                    );

                return(new TestCase <Data.Schedule, HttpRequestMessage, HttpStatusCode>
                {
                    Given = new []
                    {
                        new Data.Schedule
                        {
                            PrincipalId = claim.Id,
                            Name = claim.Schedule
                        }
                    },
                    Arguments = new HttpRequestMessage
                    {
                        Method = HttpMethod.Get,
                        RequestUri = new Uri("/api/schedule", UriKind.Relative),
                        Headers =
                        {
                            { "Authorization", $"Bearer {Auth.Issuer.Token(claim.Id.ToString())}" }
                        }
                    },
                    Expect = HttpStatusCode.OK
                });

            case "Get Existing Schedule":
                claim = new S.PrincipalClaim(
                    Guid.NewGuid(),
                    Guid.NewGuid().ToString()
                    );
                schedule = new Data.Schedule
                {
                    PrincipalId  = claim.Id,
                    Name         = claim.Schedule,
                    Appointments = Data.Arbitrary.Appointments(
                        DateTimeOffset.Now.ToUnixTimeSeconds(),
                        1800,
                        2,
                        claim.Schedule
                        ).Take(10).ToList()
                };

                return(new TestCase <Data.Schedule, HttpRequestMessage, HttpStatusCode>
                {
                    Given = new [] { schedule },
                    Arguments = new HttpRequestMessage
                    {
                        Method = HttpMethod.Get,
                        RequestUri = new Uri($"/api/schedule/{claim.Schedule}", UriKind.Relative),
                        Headers =
                        {
                            { "Authorization", $"Bearer {Auth.Issuer.Token(claim.Id.ToString())}" }
                        }
                    },
                    Expect = HttpStatusCode.OK
                });

            case "List my appointments":
                participantId = Guid.NewGuid().ToString();
                return(new TestCase <Data.Schedule, HttpRequestMessage, HttpStatusCode>
                {
                    Given = new []
                    {
                        new Data.Schedule
                        {
                            PrincipalId = Guid.NewGuid(),
                            Name = Guid.NewGuid().ToString(),
                            Appointments =
                            {
                                new Data.Appointment
                                {
                                    Start = DateTimeOffset.Now.ToUnixTimeSeconds(),
                                    Participants =
                                    {
                                        new Data.Participant {
                                            SubjectId = participantId, Name = SomeName()
                                        }
                                    }
                                }
                            }
                        }
                    },
                    Arguments = new HttpRequestMessage
                    {
                        Method = HttpMethod.Get,
                        RequestUri = new Uri("/api/appointment", UriKind.Relative),
                        Headers =
                        {
                            { HttpRequestHeader.Authorization.ToString(), $"Bearer {Auth.Issuer.Token(participantId)}" }
                        }
                    },
                    Expect = HttpStatusCode.OK
                });

            case "Get my appointment":
                participantId = Guid.NewGuid().ToString();
                scheduleName  = Guid.NewGuid().ToString();

                appointment = new Data.Appointment
                {
                    Start        = DateTimeOffset.Now.ToUnixTimeSeconds(),
                    Participants =
                    {
                        new Data.Participant {
                            SubjectId = participantId, Name = SomeName()
                        }
                    }
                };

                return(new TestCase <Data.Schedule, HttpRequestMessage, HttpStatusCode>
                {
                    Given = new []
                    {
                        new Data.Schedule
                        {
                            PrincipalId = Guid.NewGuid(),
                            Name = scheduleName,
                            Appointments =
                            {
                                appointment
                            }
                        }
                    },
                    Arguments = new HttpRequestMessage
                    {
                        Method = HttpMethod.Get,
                        RequestUri = new Uri($"/api/appointment/{scheduleName}/{appointment.Start}", UriKind.Relative),
                        Headers =
                        {
                            { HttpRequestHeader.Authorization.ToString(), $"Bearer {Auth.Issuer.Token(participantId)}" }
                        }
                    },
                    Expect = HttpStatusCode.OK
                });

            case "PostSchedule":
                claim = new S.PrincipalClaim(
                    Guid.NewGuid(),
                    Guid.NewGuid().ToString()
                    );

                var payload = Json(new { name = Guid.NewGuid().ToString() });

                return(new TestCase <Data.Schedule, HttpRequestMessage, HttpStatusCode>
                {
                    Given = new Data.Schedule[0],
                    Arguments = new HttpRequestMessage
                    {
                        Method = HttpMethod.Post,
                        RequestUri = new Uri($"/api/schedule", UriKind.Relative),
                        Headers =
                        {
                            { HttpRequestHeader.Authorization.ToString(), $"Bearer {Auth.Issuer.Token(claim.Id.ToString())}" }
                        },
                        Content = payload
                    },
                    Expect = HttpStatusCode.Created
                });

            case "PostAppointment":
                claim = new S.PrincipalClaim(
                    Guid.NewGuid(),
                    Guid.NewGuid().ToString()
                    );

                return(new TestCase <Data.Schedule, HttpRequestMessage, HttpStatusCode>
                {
                    Given = new []
                    {
                        new Data.Schedule
                        {
                            PrincipalId = claim.Id,
                            Name = claim.Schedule,
                            Appointments = Data.Arbitrary.Appointments(
                                DateTimeOffset.Now.ToUnixTimeSeconds(),
                                1800,
                                2,
                                claim.Schedule
                                ).Take(10).ToList()
                        }
                    },
                    Arguments = new HttpRequestMessage
                    {
                        Method = HttpMethod.Post,
                        RequestUri = new Uri($"/api/appointment/{claim.Schedule}/{Rng.Next()}", UriKind.Relative),
                        Headers =
                        {
                            { "Authorization", $"Bearer {Auth.Issuer.Token(claim.Id.ToString())}" }
                        },
                        Content = Json(new
                        {
                            duration = 10,
                            participants = Enumerable.Range(0, 2).Select(x => new
                            {
                                name = x.ToString(),
                                subjectId = Guid.NewGuid().ToString()
                            })
                        })
                    },
                    Expect = HttpStatusCode.Created
                });

            case "DeleteMyAppointment":
                participantId = Guid.NewGuid().ToString();
                scheduleName  = Guid.NewGuid().ToString();

                appointment = new Data.Appointment
                {
                    Start        = DateTimeOffset.Now.ToUnixTimeSeconds(),
                    Participants =
                    {
                        new Data.Participant {
                            SubjectId = participantId, Name = SomeName()
                        },
                        new Data.Participant {
                            SubjectId = Guid.NewGuid().ToString(), Name = SomeName()
                        }
                    }
                };

                return(new TestCase <Data.Schedule, HttpRequestMessage, HttpStatusCode>
                {
                    Given = new []
                    {
                        new Data.Schedule
                        {
                            PrincipalId = Guid.NewGuid(),
                            Name = scheduleName,
                            Appointments =
                            {
                                appointment,
                            }
                        }
                    },
                    Arguments = new HttpRequestMessage
                    {
                        Method = HttpMethod.Delete,
                        RequestUri = new Uri($"/api/appointment/{scheduleName}/{appointment.Start}", UriKind.Relative),
                        Headers =
                        {
                            { "Authorization", $"Bearer {Auth.Issuer.Token(participantId)}" }
                        },
                    },
                    Expect = HttpStatusCode.OK
                });

            default:
                throw new ArgumentOutOfRangeException(nameof(key));
            }
        }
        public async Task <IActionResult> CreateAppointment(
            string schedule,
            long start,
            Json.Appointment ps)
        {
            try
            {
                var claim = new S.PrincipalClaim(
                    Guid.Parse(GetSubjectId),
                    schedule);

                var appointment = new S.Appointment
                {
                    Start        = start,
                    Duration     = ps.Duration,
                    Participants = ps.Participants
                                   .Select(x => new S.Participation
                    {
                        SubjectId = x.SubjectId,
                        Name      = x.Name
                    }).ToList()
                };

                var r = await _schrepo.PostAppointment(
                    claim,
                    appointment
                    );

                if (r == S.PostAppointmentResult.ClaimNotOnRecord)
                {
                    _log.LogWarning("Invalid claim {claim}", claim);
                    return(NotFound());
                }
                if (r == S.PostAppointmentResult.Conflict)
                {
                    return(Conflict(new Json.Result
                    {
                        Success = false,
                        Message = "Already created.",
                        Links = new []
                        {
                            new Json.Link
                            {
                                Href = $"api/appointment/{schedule}",
                                Ref = "self",
                                Type = "GET"
                            }
                        }
                    }));
                }
                else
                {
                    _log.LogInformation(
                        LoggingEvents.AppointmentCreated,
                        "Appointment {appointment} created with {claim}",
                        appointment,
                        claim);

                    return(Created(
                               $"api/appointment/{schedule}",
                               new Json.Result
                    {
                        Success = true,
                        Links = new []
                        {
                            new Json.Link
                            {
                                Href = $"api/appointment/{schedule}",
                                Ref = "self",
                                Type = "GET"
                            }
                        }
                    }));
                }
            }
            catch (FormatException fmt)
            {
                _log.LogError($"Malformed sub-claim.", fmt);
                return(BadRequest());
            }
        }