Exemplo n.º 1
0
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            var connectionString = "mongodb://localhost/?safe=true";
            var server           = MongoServer.Create(connectionString);
            var db = server.GetDatabase("agencija");

            var      komentariCollection = db.GetCollection <Komentar>("komentari");
            Komentar k1 = null;

            if (tbSlika.Text == "")
            {
                k1 = new Komentar {
                    Tekst = tbTekst.Text, Ocena = Int32.Parse(tbOcena.Text), Slika = null
                };
                komentariCollection.Insert(k1);
            }
            else
            {
                k1 = new Komentar {
                    Tekst = tbTekst.Text, Ocena = Int32.Parse(tbOcena.Text), Slika = UploadSlike(tbSlika.Text)
                };
                komentariCollection.Insert(k1);
            }



            if (tip == 0)
            {
                var kolekcija = db.GetCollection <Letovanje>("putovanja");
                k1.Putovanje = new MongoDBRef("letovanja", ObjectId.Parse(idPutovanja));
                foreach (Putovanje l in kolekcija.Find(Query.EQ("_id", ObjectId.Parse(idPutovanja))))
                {
                    l.Komentari.Add(new MongoDBRef("komentari", ObjectId.Parse(k1.Id.ToString())));
                    kolekcija.Save(l);
                }
            }
            else if (tip == 1)
            {
                var kolekcija = db.GetCollection <Zimovanje>("putovanja");
                k1.Putovanje = new MongoDBRef("zimovanja", ObjectId.Parse(idPutovanja));
                foreach (Putovanje l in kolekcija.Find(Query.EQ("_id", ObjectId.Parse(idPutovanja))))
                {
                    l.Komentari.Add(new MongoDBRef("komentari", ObjectId.Parse(k1.Id.ToString())));
                    kolekcija.Save(l);
                }
            }
            else if (tip == 2)
            {
                var kolekcija = db.GetCollection <Spa>("putovanja");
                k1.Putovanje = new MongoDBRef("spa", ObjectId.Parse(idPutovanja));
                foreach (Putovanje l in kolekcija.Find(Query.EQ("_id", ObjectId.Parse(idPutovanja))))
                {
                    l.Komentari.Add(new MongoDBRef("komentari", ObjectId.Parse(k1.Id.ToString())));
                    kolekcija.Save(l);
                }
            }
            else
            {
                var kolekcija = db.GetCollection <Ekskurzija>("putovanja");
                k1.Putovanje = new MongoDBRef("ekskurzija", ObjectId.Parse(idPutovanja));
                foreach (Putovanje l in kolekcija.Find(Query.EQ("_id", ObjectId.Parse(idPutovanja))))
                {
                    l.Komentari.Add(new MongoDBRef("komentari", ObjectId.Parse(k1.Id.ToString())));
                    kolekcija.Save(l);
                }
            }


            komentariCollection.Save(k1);

            //foreach (Letovanje l in letovanjaCollection.Find(Query.EQ("_id", ObjectId.Parse(idPutovanja))))
            //{

            //    for (int i = 0; i < l.Komentari.Count(); i++)
            //    {

            //        Komentar k = db.FetchDBRefAs<Komentar>(l.Komentari[i]);
            //        if (k != null)//obavezno se ogranici jer ocigledno u startu postoje neki bezveze dbrefovi komentara koji nisu u bazi pa vrate null
            //        {
            //            MessageBox.Show(k.Tekst + " " + k.Ocena);
            //        }
            //    }
            //}


            //foreach (Komentar k in komentariCollection.FindAll())
            //{
            //   Letovanje l = db.FetchDBRefAs<Letovanje>(k.Putovanje);
            //   MessageBox.Show(l.Lokacija);
            //}
            this.Hide();
        }
        public void Init()
        {
            var       connectionString = "mongodb://localhost/?safe=true";
            var       server           = MongoServer.Create(connectionString);
            var       db = server.GetDatabase("agencija");
            Putovanje p  = new Putovanje();

            if (spa != null)
            {
                p = spa as Putovanje;
            }
            else if (zimovanje != null)
            {
                p = zimovanje as Putovanje;
            }
            else if (letovanje != null)
            {
                p = letovanje as Putovanje;
            }

            else
            {
                p = ekskurzija as Putovanje;
            }

            int i = 0;

            foreach (MongoDBRef kRef in p.Komentari)
            {
                Komentar k = db.FetchDBRefAs <Komentar>(kRef);
                if (k != null)
                {
                    if (i < 4)
                    {
                        labels[i]          = new Label();
                        labels[i].Text     = k.Ocena + ", " + k.Tekst;
                        labels[i].Location = new Point(labels[i].Location.X, labels[i].Location.Y + i * (groupBox1.Height / 6 + labels[i].Height * 2));
                        groupBox1.Controls.Add(labels[i]);


                        PictureBox pb = new PictureBox();
                        pb.SizeMode = PictureBoxSizeMode.StretchImage;
                        if (k.Slika != null)
                        {
                            byte[]       buffer    = k.Slika.ToArray();
                            MemoryStream memStream = new MemoryStream();
                            memStream.Write(buffer, 0, buffer.Length);
                            pb.Image = Image.FromStream(memStream);
                        }
                        pb.Location = new Point(labels[i].Location.X, labels[i].Location.Y + labels[i].Height);
                        pb.SizeMode = PictureBoxSizeMode.StretchImage;
                        pb.Size     = new Size(groupBox1.Width, groupBox1.Height / 6);
                        groupBox1.Controls.Add(pb);


                        chbs[i]                 = new CheckBox();
                        chbs[i].Text            = "Ukloni";
                        chbs[i].Tag             = i;
                        chbs[i].Name            = k.Id.ToString();
                        chbs[i].CheckedChanged += new EventHandler(chbs_Changed);
                        chbs[i].Location        = new Point(pb.Location.X, pb.Location.Y + pb.Height);
                        groupBox1.Controls.Add(chbs[i]);


                        i++;
                    }

                    else if (i < 8 && i >= 4)
                    {
                        labels[i]          = new Label();
                        labels[i].Text     = k.Ocena + ", " + k.Tekst;
                        labels[i].Location = new Point(labels[i].Location.X, labels[i].Location.Y + (i - 4) * 80);
                        groupBox2.Controls.Add(labels[i]);

                        PictureBox pb = new PictureBox();
                        pb.SizeMode = PictureBoxSizeMode.StretchImage;
                        if (k.Slika != null)
                        {
                            byte[]       buffer    = k.Slika.ToArray();
                            MemoryStream memStream = new MemoryStream();
                            memStream.Write(buffer, 0, buffer.Length);
                            pb.Image = Image.FromStream(memStream);
                        }
                        pb.SizeMode = PictureBoxSizeMode.StretchImage;
                        pb.Location = new Point(pb.Location.X, pb.Location.Y + (i - 4) * 80);
                        pb.Size     = new Size(groupBox1.Width, groupBox1.Height / 6);
                        groupBox2.Controls.Add(pb);

                        chbs[i]                 = new CheckBox();
                        chbs[i].Text            = "Ukloni";
                        chbs[i].Tag             = i;
                        chbs[i].Name            = k.Id.ToString();
                        chbs[i].CheckedChanged += new EventHandler(chbs_Changed);
                        chbs[i].Location        = new Point(pb.Location.X, pb.Location.Y + ((i - 4) + 1) * pb.Height);
                        groupBox2.Controls.Add(chbs[i]);


                        i++;
                    }

                    else if (i < 12 && i >= 8)
                    {
                        labels[i]          = new Label();
                        labels[i].Text     = k.Ocena + ", " + k.Tekst;
                        labels[i].Location = new Point(labels[i].Location.X, labels[i].Location.Y + (i - 8) * 80);
                        groupBox2.Controls.Add(labels[i]);

                        PictureBox pb = new PictureBox();
                        pb.SizeMode = PictureBoxSizeMode.StretchImage;
                        if (k.Slika != null)
                        {
                            byte[]       buffer    = k.Slika.ToArray();
                            MemoryStream memStream = new MemoryStream();
                            memStream.Write(buffer, 0, buffer.Length);
                            pb.Image = Image.FromStream(memStream);
                        }
                        pb.SizeMode = PictureBoxSizeMode.StretchImage;
                        pb.Location = new Point(pb.Location.X, pb.Location.Y + (i - 8) * 80);
                        pb.Size     = new Size(groupBox1.Width, groupBox1.Height / 6);
                        groupBox2.Controls.Add(pb);

                        chbs[i]                 = new CheckBox();
                        chbs[i].Text            = "Ukloni";
                        chbs[i].Tag             = i;
                        chbs[i].Name            = k.Id.ToString();
                        chbs[i].CheckedChanged += new EventHandler(chbs_Changed);
                        chbs[i].Location        = new Point(pb.Location.X, pb.Location.Y + ((i - 8) + 1) * pb.Height);
                        groupBox2.Controls.Add(chbs[i]);


                        i++;
                    }
                }
            }
        }