Пример #1
0
        public ActionResult Create(MeetingFormModel formModel)
        {
            LoadFormData();

            if (ModelState.IsValid)
            {
                var meeting = MeetingsService.Create(formModel);
                Notice("Se dit flotte møde :)");
                return(RedirectToAction("Show", new { id = meeting.Id }));
            }

            return(View(formModel));
        }
Пример #2
0
        public ActionResult Pull()
        {
            var client = new WebClient();

            client.Encoding = System.Text.Encoding.UTF8;
            var result = client.DownloadString("http://www.geekhub.dk/meetings.json?ticks=" + DateTime.Now.Ticks);
            var json   = JsonConvert.DeserializeObject <JsonViewModel>(result);

            MeetingsService.DeleteAll();

            foreach (var meeting in json.Items)
            {
                MeetingsService.Create(meeting);
            }

            return(Content("Completed"));
        }
Пример #3
0
        public async Task <ActionResult <Meeting> > PostMeeting(Meeting meeting, [FromQuery] string userId)
        {
            if (userId == null)
            {
                return(BadRequest("Missing 'userId' query parameter"));
            }

            // Check role of user
            bool isTeacher;

            try
            {
                isTeacher = await _usersService.IsTeacher(userId);
            }
            catch
            {
                return(StatusCode(500, "Couldn't check the user's role"));
            }

            // If user is a teacher, check meeting is theirs
            if (!isTeacher)
            {
                return(StatusCode(403, "Only teachers can create meetings"));;
            }

            // If user is a teacher, check meeting is theirs
            if (isTeacher && !meeting.Organizer.Equals(userId))
            {
                return(StatusCode(403, "The user making the request must match the organizer"));;
            }

            // Check all fields are valid
            BadRequestObjectResult result = ValidateFields(meeting);

            if (result != null)
            {
                return(result);
            }

            // Check organizer doesn't have any other meeting
            // overlapping in time with this one
            List <Meeting> otherMeetings = _meetingsService.GetByOrganizer(meeting.Organizer);

            foreach (Meeting other in otherMeetings)
            {
                if (TimeOverlap(meeting.StartTime, meeting.EndTime,
                                other.StartTime, other.EndTime))
                {
                    return(StatusCode(403, "The same teacher cannot have too meetings overlapping in time"));
                }
            }

            // Calculate and add intervals to meeting
            int numIntervals = meeting.TotalSpots / meeting.SpotsPerInterval;

            meeting.Intervals = new Interval[numIntervals];
            TimeSpan totalTime         = meeting.EndTime - meeting.StartTime;
            TimeSpan timePerInterval   = totalTime / numIntervals;
            DateTime intervalStartTime = meeting.StartTime;
            DateTime intervalEndTime   = intervalStartTime + timePerInterval;

            for (int i = 0; i < numIntervals; i++)
            {
                meeting.Intervals[i]           = new Interval();
                meeting.Intervals[i].StartTime = intervalStartTime;
                meeting.Intervals[i].EndTime   = intervalEndTime;
                meeting.Intervals[i].Spots     = meeting.SpotsPerInterval;
                meeting.Intervals[i].Attendees = new Booking[meeting.Intervals[i].Spots];

                intervalStartTime = intervalEndTime;
                intervalEndTime   = intervalStartTime + timePerInterval;
            }

            // Save meeting to database
            _meetingsService.Create(meeting);

            // Publish serialized event
            CreateTaskEvent _event = new CreateTaskEvent(
                "New meeting", meeting.BookingEndTime, meeting.Id);

            var options = new JsonSerializerOptions
            {
                PropertyNamingPolicy = JsonNamingPolicy.CamelCase
            };
            string jsonString = JsonSerializer.Serialize(_event, options);

            _msgQueueService.ProduceMessage(jsonString);

            return(CreatedAtAction("GetMeeting", new { id = meeting.Id.ToString() }, meeting));
        }