Ejemplo n.º 1
0
        public static IEnumerable <Tyontekija> getAllWithPermissions()
        {
            using (var conn = Database.connection())
            {
                conn.Open();
                string sql = @"SELECT t.id, t.nimi, tyontekijaryhma, tyopiste_id, tp.nimi AS tyopiste FROM tyontekija t
                        INNER JOIN luvat l ON t.id = l.tyontekija_id INNER JOIN tyopiste tp ON l.tyopiste_id = tp.id";
                using (var command = new NpgsqlCommand(sql, conn))
                {
                    NpgsqlDataReader  reader      = command.ExecuteReader();
                    Tyontekija        t           = null;
                    List <Tyontekija> tyontekijat = new List <Tyontekija>();
                    List <Tyopiste>   tyopisteet  = new List <Tyopiste>(Tyopiste.all());

                    if (t == null)
                    {
                        reader.Read();
                        t = new Tyontekija((int)reader[0], (String)reader[1], (String)reader[2]);
                        tyopisteet.Find(x => x.Id == new Tyopiste {
                            Id = (int)reader[3], Nimi = (string)reader[4]
                        }.Id).Allowed = true;
                    }
                    while (reader.Read())
                    {
                        if (t.Id == (int)reader[0])
                        {
                            tyopisteet.Find(x => x.Id == new Tyopiste {
                                Id = (int)reader[3], Nimi = (string)reader[4]
                            }.Id).Allowed = true;

                            {
                            };
                        }
                        else
                        {
                            t.Tyopisteet = tyopisteet;
                            tyopisteet   = new List <Tyopiste>(Tyopiste.all());
                            tyontekijat.Add(t);
                            t = new Tyontekija((int)reader[0], (String)reader[1], (String)reader[2]);
                            tyopisteet.Find(x => x.Id == new Tyopiste {
                                Id = (int)reader[3], Nimi = (string)reader[4]
                            }.Id).Allowed = true;
                        }
                    }
                    t.Tyopisteet = tyopisteet;
                    tyontekijat.Add(t);



                    return(tyontekijat);
                }
            }
        }
Ejemplo n.º 2
0
        public static List <Tyontekija> all()
        {
            using (var conn = Database.connection())
            {
                conn.Open();
                using (var command = new NpgsqlCommand("SELECT * FROM tyontekija", conn))
                {
                    List <Tyontekija> l = new List <Tyontekija>();
                    Tyontekija        t = null;

                    NpgsqlDataReader reader = command.ExecuteReader();
                    while (reader.Read())
                    {
                        t            = new Tyontekija((int)reader[0], (String)reader[1], (String)reader[2]);
                        t.Tyopisteet = new List <Tyopiste>(Tyopiste.all());
                        l.Add(t);
                    }
                    return(l);
                }
            }
        }
Ejemplo n.º 3
0
        public static Tyontekija findWithPermissions(int id)
        {
            using (var conn = Database.connection())
            {
                conn.Open();
                string sql = @"SELECT t.id, t.nimi, tyontekijaryhma, tyopiste_id, tp.nimi AS tyopiste FROM tyontekija t
                        INNER JOIN luvat l ON t.id = l.tyontekija_id INNER JOIN tyopiste tp ON l.tyopiste_id = tp.id WHERE t.id = @id";

                using (var command = new NpgsqlCommand(sql, conn))
                {
                    command.Parameters.AddWithValue("id", id);
                    NpgsqlDataReader reader     = command.ExecuteReader();
                    Tyontekija       t          = null;
                    List <Tyopiste>  tyopisteet = new List <Tyopiste>(Tyopiste.all());

                    if (!reader.Read())
                    {
                        t = find(id);
                    }
                    else
                    {
                        t = new Tyontekija((int)reader[0], (String)reader[1], (String)reader[2]);
                        tyopisteet.Find(x => x.Id == new Tyopiste {
                            Id = (int)reader[3], Nimi = (string)reader[4]
                        }.Id).Allowed = true;
                        while (reader.Read())
                        {
                            tyopisteet.Find(x => x.Id == new Tyopiste {
                                Id = (int)reader[3], Nimi = (string)reader[4]
                            }.Id).Allowed = true;
                        }
                    }
                    t.Tyopisteet = tyopisteet;
                    return(t);
                }
            }
        }