Exemplo n.º 1
0
        // POST api/<controller>
        //
        // Add user to the database
        //
        public HttpResponseMessage Post([FromBody] JsonUser value)
        {
            if (value == null)
            {
                Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid user information");
            }

            Models.Repository     repository = new Models.Repository();
            Facebook.FacebookUser fbUser     = new Facebook.FacebookUser
            {
                id         = value.UserId,
                email      = value.Email,
                first_name = value.FirstName,
                last_name  = value.LastName,
                name       = value.Name
            };
            var mfUser = repository.CreateUser(fbUser);

            repository.SaveChanges();

            List <GroupMember> membership = repository.UserMembership(mfUser);

            if ((membership == null) || (membership.Count == 0))
            {
                Group defaultGroup = repository.FindGroupByCode("JYMF");
                repository.DefaultGroup(mfUser, defaultGroup, GroupRoleEnum.member);
                repository.SaveChanges();
            }


            return(Request.CreateResponse(HttpStatusCode.Created, "User added to application"));
        }
Exemplo n.º 2
0
        // POST api/<controller>
        //
        // Create a new event.  Json eventInfo is passed in through the body of the POST
        //
        public HttpResponseMessage Post([FromBody] JsonEvent eventInfo)
        {
            // Make sure the request is valid
            //
            if (string.IsNullOrEmpty(UserId))
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Not Authorized"));
            }

            if (eventInfo == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotAcceptable, "Invalid Event Information"));
            }

            // Create new Model Event
            //
            RaceDay.Models.Event iEvent = eventInfo.ToDatabase();
            iEvent.GroupId   = GroupId;
            iEvent.CreatorId = UserId;

            // Add to the database and then add this user as a participant
            //
            Repository repository = new Repository();
            Event      newEvent   = repository.AddEvent(iEvent);

            repository.SaveChanges();

            if ((newEvent != null) && (newEvent.EventId > 0))
            {
                var user = repository.GetUserById(UserId);
                repository.AddUserToEvent(user, newEvent, AttendingEnum.Attending);
                repository.SaveChanges();

                var addedEvent     = repository.GetEventViewById(newEvent.EventId, UserId);
                var eventAttendees = repository.GetUsersForEvent(newEvent.EventId);
                return(Request.CreateResponse(HttpStatusCode.Created, new { eventinfo = addedEvent, attendees = JsonUser.FromDatabase(eventAttendees) }));
            }

            return(Request.CreateResponse(HttpStatusCode.NotAcceptable, "Unable to create event"));
        }
Exemplo n.º 3
0
        // GET api/<controller>/5
        //
        // Return detailed event information along with all users attending the event
        //
        public HttpResponseMessage Get(int id)
        {
            if (string.IsNullOrEmpty(UserId))
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Not Authorized"));
            }

            Repository repository = new Repository();
            var        eventInfo  = repository.GetEventViewById(id, UserId);

            if (eventInfo != null)
            {
                var eventAttendees = repository.GetUsersForEvent(eventInfo.EventId);
                return(Request.CreateResponse(HttpStatusCode.OK, new { eventinfo = eventInfo, attendees = JsonUser.FromDatabase(eventAttendees) }));
            }
            else
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, string.Format("Event {0} not found", id)));
            }
        }