Beispiel #1
0
        [Route("{keycode}")]  // osoite on host/documentation/keycode
        public ActionResult Documentation(string keycode)
        {
            northwindContext db = new northwindContext();

            try
            {
                var isInDatabase = db.Documentations.Any(r => r.Keycode == keycode);

                if (isInDatabase) // jos koodi löytyy tietokannasta
                {
                    List <Documentation> lista = (from x in db.Documentations
                                                  where x.Keycode == keycode
                                                  select x).ToList();
                    return(Ok(lista));
                }
                else
                {
                    return(BadRequest("Empty " + DateTime.Now.ToString()));
                }
            }
            catch (Exception)
            {
                return(BadRequest("Virhe."));
            }
            finally
            {
                db.Dispose();  // vapauttaa db objektin lopuksi
            }
        }
Beispiel #2
0
        public Customers GetOneCustomer(string id)
        {
            northwindContext db      = new northwindContext();
            Customers        asiakas = db.Customers.Find(id);

            return(asiakas);
        }
        public ActionResult GetProduct(int primarykey)
        {
            northwindContext db = new northwindContext();

            try
            {
                var tuote = db.Products.Find(primarykey);

                if (tuote != null)
                {
                    return(Ok(tuote));
                }
                else
                {
                    return(NotFound("Ei löytynyt"));
                }
            }
            catch (Exception)
            {
                return(BadRequest());
            }
            finally
            {
                db.Dispose();
            }
        }
        public List <Logins> GetAllUsers() //Hakee kaikki rivit
        {
            northwindContext db    = new northwindContext();
            List <Logins>    users = db.Logins.ToList();

            return(users);
        }
Beispiel #5
0
        public List <Customers> GetAllCustomers()
        {
            northwindContext db        = new northwindContext();
            List <Customers> asiakkaat = db.Customers.ToList();

            return(asiakkaat);
        }
        public List <PublicDocument> Get(string key)
        {
            northwindContext context = new northwindContext();

            List <Documentation> privateDocList = (from d in context.Documentation
                                                   where d.Keycode == key
                                                   select d).ToList();
            //return privateDocList;

            List <PublicDocument> publicDocList = new List <PublicDocument>();

            foreach (Documentation privateDoc in privateDocList)
            {
                PublicDocument publicDoc = new PublicDocument();
                publicDoc.DocumentationId = privateDoc.DocumentationId;
                publicDoc.AvailableRoute  = privateDoc.AvailableRoute;
                publicDoc.Method          = privateDoc.Method;
                publicDoc.Description     = privateDoc.Description;
                publicDocList.Add(publicDoc);
            }
            if (publicDocList.Count == 0)
            {
                PublicDocument publicDoc = new PublicDocument();
                publicDoc.DocumentationId = 0;
                publicDoc.AvailableRoute  = DateTime.Now.ToString();
                publicDoc.Method          = "Documentation missing";
                publicDoc.Description     = "Empty";
                publicDocList.Add(publicDoc);
            }

            return(publicDocList);
        }
        public ActionResult GetSomeProducts(int id)
        {
            northwindContext db = new northwindContext();

            try
            {
                var isInDatabase = db.Products.Any(s => s.SupplierId == id);

                if (isInDatabase)
                {
                    var tuotteet = from p in db.Products
                                   where p.SupplierId == id
                                   select p;

                    var tuotelista = tuotteet.ToList();

                    return(Ok(tuotelista));
                }
                else
                {
                    return(NotFound("Toimittajakoodilla ei löytynyt toimittajaa."));
                }
            }
            catch
            {
                return(BadRequest());
            }
            finally
            {
                db.Dispose();
            }
        }
Beispiel #8
0
        [Route("{primarykey}")]                                                           // Routemääritys tietokannan pääavaimella /api/documentation/pääavain
        public ActionResult PutEdit(int primarykey, [FromBody] Documentation uudetTiedot) // [FromBody] = HTTP-pyynnön Body:ssä välitetään JSON-muodossa oleva objekti ,joka on Documentation-tyyppinen uudetTiedot-niminen
        {
            northwindContext db = new northwindContext();

            try
            {
                Documentation muokattava = db.Documentations.Find(primarykey);   // etsitään tietokannasta pääavaimella
                if (muokattava != null)                                          // jos löytyy niin siihen sijoitetaan tuodut tiedot
                {
                    muokattava.AvailableRoute = uudetTiedot.AvailableRoute;
                    muokattava.Method         = uudetTiedot.Method;
                    muokattava.Description    = uudetTiedot.Description;
                    muokattava.Keycode        = uudetTiedot.Keycode;

                    db.SaveChanges();
                    return(Ok("Muutokset tallennettu."));
                }
                else
                {
                    return(NotFound("Ei löytynyt!"));
                }
            }
            catch (Exception e)
            {
                return(BadRequest("Virhe päivitettäessä." + e));
            }
            finally
            {
                db.Dispose();
            }
        }
Beispiel #9
0
        public ActionResult DeleteEmployee(int id)
        {
            northwindContext db = new northwindContext();

            try
            {
                Employees Employee = db.Employees.Find(id);
                if (Employee != null)
                {
                    db.Employees.Remove(Employee);
                    db.SaveChanges();
                    return(Ok("Henkilö id:lla " + id + " poistettiin"));
                }
                else
                {
                    return(NotFound("Henkilöä id:lla" + id + " ei loydy"));
                }
            }
            catch
            {
                return(BadRequest());
            }
            finally
            {
                db.Dispose();
            }
        }
        public ActionResult Delete(int primarykey)
        {
            northwindContext db = new northwindContext();

            try
            {
                Employee poistettava = db.Employees.Find(primarykey);
                if (poistettava != null)
                {
                    db.Employees.Remove(poistettava);
                    db.SaveChanges();
                    return(Ok("Työntekijä id " + poistettava.EmployeeId + " poistettu."));
                }
                else
                {
                    return(NotFound("Työntekijää ei löytynyt."));
                }
            }
            catch
            {
                return(BadRequest("Virhe poistettaessa."));
            }
            finally
            {
                db.Dispose();
            }
        }
        public ActionResult UpdateProduct(int id, [FromBody] Products newProd)
        {
            northwindContext db = new northwindContext();

            try
            {
                Products oldProd = db.Products.Find(id);
                if (oldProd != null)
                {
                    oldProd.ProductName     = newProd.ProductName;
                    oldProd.SupplierId      = newProd.SupplierId;
                    oldProd.CategoryId      = newProd.CategoryId;
                    oldProd.QuantityPerUnit = newProd.QuantityPerUnit;
                    oldProd.UnitsInStock    = newProd.UnitsInStock;
                    oldProd.UnitsOnOrder    = newProd.UnitsOnOrder;
                    oldProd.ReorderLevel    = newProd.ReorderLevel;
                    oldProd.Discontinued    = newProd.Discontinued;

                    db.SaveChanges();
                    return(Ok(newProd.ProductId));
                }
                else
                {
                    return(NotFound("Tuotetta ei löydy!"));
                }
            }
            catch (Exception)
            {
                return(BadRequest("Tuotteen tietojen päivittäminen ei onnistunut."));
            }
            finally
            {
                db.Dispose();
            }
        }
        public ActionResult GetSomeEmployees(string country)
        {
            northwindContext db = new northwindContext();

            try
            {
                var isInDatabase = db.Employees.Any(s => s.Country == country);

                if (isInDatabase)
                {
                    var employees = from e in db.Employees
                                    where e.Country == country
                                    select e;

                    var emplist = employees.ToList();

                    return(Ok(emplist));
                }
                else
                {
                    return(NotFound("Tällä maalla ei löytynyt henkilöitä."));
                }
            }
            catch
            {
                return(BadRequest());
            }
            finally
            {
                db.Dispose();
            }
        }
        public ActionResult GetEmployee(int primarykey)
        {
            northwindContext db = new northwindContext();

            try
            {
                var employee = db.Employees.Find(primarykey);

                if (employee != null)
                {
                    return(Ok(employee));
                }
                else
                {
                    return(NotFound("Ei löytynyt"));
                }
            }
            catch (Exception)
            {
                return(BadRequest());
            }
            finally
            {
                db.Dispose();
            }
        }
        [Route("{customerid}")] // Routemääritys pääavaimella /nw/customer/pääavain
        public ActionResult DeleteCustomer(string customerid)
        {
            northwindContext db = new northwindContext();

            try
            {
                Customer poistettava = db.Customers.Find(customerid); // etsitään asiakas tietokannasta pääavaimella
                if (poistettava != null)                              // jos asiakas löytyy niin yritetään poistoa
                {
                    try
                    {
                        db.Customers.Remove(poistettava);
                        db.SaveChanges();
                        return(Ok("Asiakas id " + customerid + " poistettu."));
                    }
                    catch (Exception)
                    {
                        return(BadRequest("Virhe asiakasta poistettaessa."));
                    }
                }
                else
                {
                    return(NotFound("Asiakasta ei löytynyt."));
                }
            }
            finally
            {
                db.Dispose();
            }
        }
Beispiel #15
0
        [Route("")]                       // esimerkkinä routen paikka vaikkakin tässä tyhjä
        public ActionResult GetAllUsers() //Hakee kaikki rivit
        {
            northwindContext db = new northwindContext();

            try
            {
                var users = from u in db.Logins
                            select new Login
                {
                    LoginId       = u.LoginId,
                    UserName      = u.UserName,
                    Firstname     = u.Firstname,
                    Lastname      = u.Lastname,
                    Email         = u.Email,
                    AccessLevelId = u.AccessLevelId
                };

                var loginsList = users.ToList();

                return(Ok(loginsList));
            }
            catch (Exception)
            {
                return(BadRequest());
            }
            finally
            {
                db.Dispose();
            }
        }
Beispiel #16
0
        public ActionResult Deleteorder(int id)
        {
            northwindContext db = new northwindContext();

            try
            {
                Orders order = db.Orders.Find(id);
                if (order != null)
                {
                    db.Orders.Remove(order);
                    db.SaveChanges();
                    return(Ok("Tilaus id:llä " + id + " poistettiin"));
                }
                else
                {
                    return(NotFound("Tilausta id:llä" + id + " ei löydy"));
                }
            }
            catch
            {
                return(BadRequest());
            }
            finally
            {
                db.Dispose();
            }
        }
Beispiel #17
0
        [Route("{primarykey}")] // Routemääritys tietokannan pääavaimella /api/documentation/pääavain
        public ActionResult Delete(int primarykey)
        {
            northwindContext db = new northwindContext();

            try
            {
                Documentation poistettava = db.Documentations.Find(primarykey);  // etsitään tietokannasta pääavaimella
                if (poistettava != null)                                         // jos löytyy niin yritetään poistoa
                {
                    try
                    {
                        db.Documentations.Remove(poistettava);
                        db.SaveChanges();
                        return(Ok("Dokumentti id " + poistettava.DocumentationId + " poistettu."));
                    }
                    catch (Exception)
                    {
                        return(BadRequest("Virhe poistettaessa."));
                    }
                }
                else
                {
                    return(NotFound("Dokumenttiä ei löytynyt."));
                }
            }
            finally
            {
                db.Dispose();
            }
        }
Beispiel #18
0
        public ActionResult PostCreateNew([FromBody] Login uusiuser)
        {
            northwindContext db = new northwindContext();

            try
            {
                uusiuser.PassWord      = BCrypt.Net.BCrypt.HashPassword(uusiuser.PassWord); // Tässä hashataan salana Brycptilla kantaan
                uusiuser.UserName      = uusiuser.UserName;
                uusiuser.Firstname     = uusiuser.Firstname;
                uusiuser.Lastname      = uusiuser.Lastname;
                uusiuser.Email         = uusiuser.Email;
                uusiuser.AccessLevelId = uusiuser.AccessLevelId;
                uusiuser.Token         = "";

                db.Logins.Add(uusiuser);
                db.SaveChanges();
                return(Ok("Käyttäjä lisätty id:llä " + uusiuser.LoginId));
            }
            catch (Exception)
            {
                return(BadRequest("Virhe lisättäessä."));
            }
            finally
            {
                db.Dispose();
            }
        }
        public ActionResult DeleteProduct(int id)
        {
            northwindContext db = new northwindContext();

            try
            {
                Products product = db.Products.Find(id);
                if (product != null)
                {
                    db.Products.Remove(product);
                    db.SaveChanges();
                    return(Ok("Tuote id:llä " + id + " poistettiin"));
                }
                else
                {
                    return(NotFound("Tuotetta id:llä" + id + " ei löydy"));
                }
            }
            catch
            {
                return(BadRequest());
            }
            finally
            {
                db.Dispose();
            }
        }
        public IActionResult GetSomeCustomers(int offset, int limit, string country)
        {
            northwindContext db = new northwindContext();

            try
            {
                if (country != null) // Jos parametri ei ole null eli on annettu
                {
                    List <Customer> asiakkaat = db.Customers.Where(c => c.Country == country).Take(limit).ToList();
                    return(Ok(asiakkaat));
                }
                else
                {
                    List <Customer> asiakkaat = db.Customers.Skip(offset).Take(limit).ToList();
                    return(Ok(asiakkaat));
                }
            }
            catch
            {
                return(BadRequest("Virhe."));
            }
            finally
            {
                db.Dispose();
            }
        }
        public Customer GetOneCustomer(string id) //Find-metodi hakee aina vain PÄÄAVAIMELLA yhden tuloksen
        {
            northwindContext db      = new northwindContext();
            Customer         asiakas = db.Customers.Find(id);

            db.Dispose();
            return(asiakas);
        }
        [Route("")]                                              // esimerkkinä routen paikka vaikkakin tässä tyhjä
        public List <Customer> GetAllCustomers()                 //Hakee kaikki rivit
        {
            northwindContext db        = new northwindContext(); // tietokantayhteys
            List <Customer>  asiakkaat = db.Customers.ToList();  // tietokannaan customers sisältö asiakkaat nimiselle listalle

            db.Dispose();                                        // tietokantayhteysolio pois
            return(asiakkaat);                                   // palautetaan luotu lista
        }
Beispiel #23
0
 public Bets EfGetBetById()
 {
     using (var db = new northwindContext())
     {
         long betId = 100;
         return(db.Bets.Find(betId));
     }
 }
        public List <Products> GetAllProducts()
        {
            northwindContext db       = new northwindContext();
            List <Products>  products = db.Products.ToList();

            db.Dispose();
            return(products);
        }
Beispiel #25
0
 public Bets EfGetBetByIdNoTracking()
 {
     using (var db = new northwindContext())
     {
         long betId = 100;
         return(db.Bets.AsNoTracking().First(p => p.BetId == betId));
     }
 }
Beispiel #26
0
        public List <Categories> GetAllCategories()
        {
            northwindContext  db         = new northwindContext();
            List <Categories> categories = db.Categories.ToList();

            db.Dispose();
            return(categories);
        }
        public List <Employees> GetAllEmployees()
        {
            northwindContext db        = new northwindContext();
            List <Employees> employees = db.Employees.ToList();

            db.Dispose();
            return(employees);
        }
Beispiel #28
0
        public List <Orders> GetAllOrders()
        {
            northwindContext db     = new northwindContext();
            List <Orders>    orders = db.Orders.ToList();

            db.Dispose();
            return(orders);
        }
        [Route("")]                                                //<--tyhjä reitinmääritys (ei ole pakko laittaa), eli ei mitään lisättävää reittiin, jolloin
        public string PostCreateNew([FromBody] Documentation doku) //<-- [FromBody] tarkoittaa, että HTTP-pyynnön Body:ssä välitetään JSON-muodossa oleva objekti ,joka om Documentation-tyyppinen customer-niminen
        {
            northwindContext context = new northwindContext();     //Context = Kuten entities muodostettu Scaffold DBContext -tykalulla. Voisi olla myös entiteetti frameworkCore

            context.Documentation.Add(doku);
            context.SaveChanges();

            return(doku.DocumentationId.ToString()); //kuittaus Frontille, että päivitys meni oikein --> Frontti voi tsekata, että kontrolleri palauttaa saman id:n mitä käsitteli
        }
        public List <Products> GetByPrice(int min, int max)
        {
            northwindContext db = new northwindContext();
            var tuotteet        = from p in db.Products
                                  where p.UnitPrice > min && p.UnitPrice < max
                                  select p;

            return(tuotteet.ToList());
        }