예제 #1
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);
        }
예제 #2
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
         });
        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());
            }
        }