Exemplo n.º 1
0
        public GeneralVM()
        {
            if (Festival.GetFestivals() != null)
            {
                if (Festival.GetFestivals().Count > 0)
                {
                    Festival = Festival.GetFestivals()[0];
                    _OldFestival = new Festival() {ID = Festival.ID, Name = Festival.Name, Street = Festival.Street, City = Festival.City};

                    // There's data to display, so disable the textboxes and show edit button
                    ShowEdit = "Visible";
                    ShowCancel = "Hidden";
                    ShowSave = "Hidden";
                    Enabled = false;

                    return;
                }
            }

            _OldFestival = null;
            // Else enable the textboxes and show the save button
            ShowEdit = "Hidden";
            ShowCancel = "Hidden";
            ShowSave = "Visible";
            Enabled = true;
        }
Exemplo n.º 2
0
        public static void UpdateFestival(Festival festival)
        {
            // If _Festivals is null, create the Observable Collection
            if (_Festivals == null) GetFestivals();

            try
            {
                // Update db
                DbParameter param1 = Database.AddParameter("@id", festival.ID);
                DbParameter param2 = Database.AddParameter("@name", festival.Name);
                DbParameter param3 = Database.AddParameter("@street", festival.Street);
                DbParameter param4 = Database.AddParameter("@city", festival.City);
                int affectedRows = Database.ModifyData("UPDATE festival SET name = @name, street = @street, city = @city WHERE id = @id", param1, param2, param3, param4);
                if (affectedRows == 0) return;

                // Update _Festivals
                for (int i = 0; i < _Festivals.Count; ++i)
                {
                    if (_Festivals[i].ID == festival.ID)
                    {
                        _Festivals[i] = festival;
                        i = _Festivals.Count;
                    }
                }
            }

            // Fail
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
Exemplo n.º 3
0
        public static ObservableCollection<Festival> GetFestivals()
        {
            // If _Festivals is null, create the Observable Collection
            if (_Festivals == null)
            {
                try
                {
                    // Create _Festivals
                    _Festivals = new ObservableCollection<Festival>();

                    // Get data
                    DbDataReader reader = Database.GetData("SELECT * FROM festival");
                    foreach (DbDataRecord record in reader)
                    {
                        // Create new Festival
                        Festival festival = new Festival();

                        // Get ID
                        if (DBNull.Value.Equals(record["ID"])) festival.ID = -1;
                        else festival.ID = Convert.ToInt32(record["ID"]);

                        // Get Name
                        if (DBNull.Value.Equals(record["Name"])) festival.Name = "";
                        else festival.Name = record["Name"].ToString();

                        // Get Street
                        if (DBNull.Value.Equals(record["Street"])) festival.Street = "";
                        else festival.Street = record["Street"].ToString();

                        // Get City
                        if (DBNull.Value.Equals(record["City"])) festival.City = "";
                        else festival.City = record["City"].ToString();

                        // Add Festival
                        _Festivals.Add(festival);
                    }
                    if (reader != null)
                        reader.Close();
                }

                // Fail
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);

                    // Clear Festivals
                    _Festivals.Clear();
                    _Festivals = null;
                }

            }

            // Return Festivals
            return _Festivals;
        }
Exemplo n.º 4
0
        public static int AddFestival(Festival festival)
        {
            // If _Festivals is null, create the Observable Collection
            if (_Festivals == null) GetFestivals();

            try
            {
                // Add to db
                DbParameter param1 = Database.AddParameter("@name", festival.Name);
                DbParameter param2 = Database.AddParameter("@street", festival.Street);
                DbParameter param3 = Database.AddParameter("@city", festival.City);
                DbDataReader reader = Database.GetData("INSERT INTO festival(Name, Street, City) VALUES(@name, @street, @city);SELECT LAST_INSERT_ID() AS ID", param1, param2, param3);
                foreach (DbDataRecord record in reader)
                {
                    // Get ID
                    if (DBNull.Value.Equals(record["ID"])) festival.ID = -1;
                    else festival.ID = Convert.ToInt32(record["ID"]);
                }
                if (reader != null)
                    reader.Close();

                _Festivals.Add(festival);
                return festival.ID;
            }

            // Fail
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return -1;
        }
Exemplo n.º 5
0
 private void SaveUpdateFestival(GeneralVM generalvm)
 {
     if (!Festival.IsValid())
         return;
     // Save Changes
     if (_OldFestival == null)
     {
         // Insert into db
         Festival.AddFestival(Festival);
         if (Festival.GetFestivals() != null)
         {
             if (Festival.GetFestivals().Count > 0)
             {
                 Festival = Festival.GetFestivals()[0];
                 _OldFestival = Festival;
             }
         }
     } else {
         // Update db
         Festival.UpdateFestival(Festival);
         _OldFestival = new Festival() { ID = Festival.ID, Name = Festival.Name, Street = Festival.Street, City = Festival.City };
     }
     // Update GUI
     generalvm.ShowEdit = "Visible";
     generalvm.ShowCancel = "Hidden";
     generalvm.ShowSave = "Hidden";
     Enabled = false;
 }