Beispiel #1
0
        public EventViewModels CreateEvent(Container_Classes.Event NewEvent)
        {
            int typeID = int.Parse(NewEvent.Type);

            // Convert the type string and category string to database keys
            Data.Type dataType = _repository.Get <Data.Type>(x => x.TypeID == typeID);
            if (dataType == null)
            {
                // Could not map type to the database
                return(null);
            }

            Data.Category dataCategory = _repository.Get <Data.Category>(x => x.Category1.Equals(NewEvent.Category));

            // Convert the Container Event into a Data Event so it can be added to the database
            Data.Event dataEvent = Container_Classes.Event.ContainerEventToDataEvent(NewEvent, dataCategory.CategoryID, dataType.TypeID, NewEvent.Owner_ID);
            _repository.Add <Data.Event>(dataEvent);
            _repository.SaveChanges();

            // Weak way of getting new event, this should break if two events have the same name!
            dataEvent = _repository.Get <Data.Event>(x => x.Title == NewEvent.Title);

            // Assign the ID to the added container object event
            NewEvent.ID = dataEvent.EventID;

            EventViewModels model = new EventViewModels();

            model.Event = NewEvent;

            return(model);
        }
Beispiel #2
0
        public EventViewModels UpdateEvent(Container_Classes.Event UpdatedEvent)
        {
            // Retreieve the existing event from the data to update it.
            Data.Event dataEvent = _repository.Get <Data.Event>(x => x.EventID == UpdatedEvent.ID);
            if (dataEvent == null)
            {
                // The event does not exist in the database.
                return(EventNotFound());
            }

            // Convert the type string and category string to database keys
            Data.Type dataType = _repository.Get <Data.Type>(x => x.Type1.Equals(UpdatedEvent.Type));
            if (dataType == null)
            {
                // Could not map type to the database
                return(null);
            }


            // The event exists and we have it stored as data event. Let's update it.
            dataEvent = Container_Classes.Event.ContainerEventToDataEvent(UpdatedEvent, 1, dataType.TypeID, UpdatedEvent.Owner_ID);
            _repository.Update <Data.Event>(dataEvent);

            _repository.SaveChanges();

            EventViewModels model = new EventViewModels();

            model.Event = UpdatedEvent;

            return(model);
        }
Beispiel #3
0
        public EventViewModels EventNotFound()
        {
            // This could break if we have multiple events with the same name
            Data.Event dataEvent = _repository.Get <Data.Event>(x => x.Title.Equals("NOTFOUND", StringComparison.Ordinal));
            if (dataEvent == null)
            {
                // We can't even find the fake event!
                return(null);
            }
            Container_Classes.Event containerEvent = Container_Classes.Event.DataEventToContainerEvent(dataEvent, 0, "NOTFOUND", "NOTFOUND");

            EventViewModels model = new EventViewModels();

            model.Event = containerEvent;

            return(model);
        }
Beispiel #4
0
        public EventViewModels CancelEvent(Container_Classes.Event containerEvent)
        {
            Data.Event dataEvent = _repository.Get <Data.Event>(x => x.EventID == containerEvent.ID);
            if (dataEvent == null)
            {
                // We did not find the event to cancel through the id
                return(EventNotFound());
            }

            // Change the status to cancelled :(
            dataEvent.Status = "Cancelled";
            // Push the update back to the database
            _repository.Update <Data.Event>(dataEvent);
            // Make sure we save our work!
            _repository.SaveChanges();

            // Returned the updated event back to the view

            EventViewModels model = new EventViewModels();

            model.Event = containerEvent;

            return(model);
        }
Beispiel #5
0
        // Returns the view of all of the events the user is registered for.
        public EventsViewModels RegisterUserForEvent(Container_Classes.Event containerEvent, Container_Classes.User containerUser)
        {
            int foodID = int.Parse(containerUser.Foods);

            // Get the food ID from the database using the containerUser object
            Data.Food dataFood = _repository.Get <Data.Food>(x => x.FoodID == foodID);
            // Ensure that we found the food in the database
            if (dataFood == null)
            {
                return(null);
            }

            // Ensure that the user exists in the database
            Data.User dataUser = Container_Classes.User.UserToDataUser(containerUser, dataFood.FoodID);
            dataUser = _repository.Get <Data.User>(x => x.UserID == dataUser.UserID);

            if (dataUser == null)
            {
                // The user was not found in the database
                return(null);
            }

            // Grab the Category and type from the database so we can read an event from the database
            Data.Category dataCategory = _repository.Get <Data.Category>(x => x.Category1.Equals(containerEvent.Category));
            if (dataCategory == null)
            {
                // We could not find the category in the database
                return(null);
            }

            Data.Type dataType = _repository.Get <Data.Type>(x => x.Type1.Equals(containerEvent.Type));
            if (dataType == null)
            {
                // We could not find the type in the database
                return(null);
            }

            // Ensure that the event exists in the database
            Data.Event dataEvent = Container_Classes.Event.ContainerEventToDataEvent(containerEvent, dataCategory.CategoryID, dataType.TypeID, dataUser.UserID);
            dataEvent = _repository.Get <Data.Event>(x => x.EventID == dataEvent.EventID);

            if (dataEvent == null)
            {
                // The user was not found in the database
                return(null);
            }

            // Prepare the information to be updated in the database
            Data.Registration registration = new Data.Registration();
            registration.User_ID  = dataUser.UserID;
            registration.Event_ID = dataEvent.EventID;

            // Add the registration to the database
            _repository.Add <Data.Registration>(registration);

            _repository.SaveChanges();

            // Get all of the users registered events
            List <Data.Registration> registrations = DatabaseToDataRegistration(_repository.GetAll <Data.Registration>(x => x.User_ID == dataUser.UserID));
            List <Data.Event>        dataEvents    = new List <Data.Event>();

            // Create a list of all events this user is attending
            foreach (Data.Registration dataRegistration in registrations)
            {
                dataEvents.Add(_repository.Get <Data.Event>(x => x.EventID == dataRegistration.Event_ID));
            }

            // Conver the list of dataEvents to containerEvents
            List <Container_Classes.Event> containerEvents = new List <Container_Classes.Event>();

            foreach (Data.Event dataEvent2 in dataEvents)
            {
                dataType     = _repository.Get <Data.Type>(x => x.TypeID == dataEvent2.Type_ID);
                dataCategory = _repository.Get <Data.Category>(x => x.CategoryID == dataEvent2.Category_ID);
                containerEvents.Add(Container_Classes.Event.DataEventToContainerEvent(dataEvent2, dataUser.UserID, dataCategory.Category1, dataType.Type1));
            }

            EventsViewModels model = new EventsViewModels();

            model.Events = containerEvents;

            return(model);
        }