Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
6.1 kB
8
Indexable
Never
using SOMIOD;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;



namespace SOMIOD1.Controllers
{

    public class MainController : ApiController
    {
        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["SOMIOD1.Properties.Settings.ConnStr"].ConnectionString;

        // GET: api/somiod/<controller>
        [HttpGet]
        [Route("api/somiod/{applicationName}")]

        public IEnumerable<Module> GetModules(string applicationName)
        {
            List<Module> listModules = new List<Module>();
            string sql = "SELECT * FROM Modules";
            SqlConnection conn = null;

            try
            {
                conn = new SqlConnection(connectionString);
                conn.Open();

                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    Module module = new Module
                    {
                        Id = (int)reader["Id"],
                        Name = (string)reader["Name"],
                        Creation_dt = (DateTime)reader["Creation_dt"],
                        Parent = (int)reader["Parent"]
                    };
                    listModules.Add(module);
                }
                reader.Close();
                conn.Close();
            }

            catch (Exception)
            {

                if (conn.State == System.Data.ConnectionState.Open)
                    conn.Close();
            }

            return listModules;

        }

        // GET: api/somiod/<controler>/5
        [HttpGet]
        [Route("api/somiod/{applicationName}/{id:int}")]

        public IHttpActionResult Get(int id, string applicationName)
        {
            string sql = "SELECT * FROM Modules WHERE Id=@id";
            SqlConnection conn = null;
            Module module = null;

            try
            {
                conn = new SqlConnection(connectionString);
                conn.Open();

                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@id", id);
                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    module = new Module
                    {
                        Id = (int)reader["Id"],
                        Name = (string)reader["Name"],
                        Creation_dt = (DateTime)reader["Creation_dt"],
                        Parent = (int)reader["Parent"]

                    };

                }
                reader.Close();
                conn.Close();

                if (module == null)
                {
                    return NotFound();
                }
                return Ok(module);
            }
            catch (Exception)
            {

                if (conn.State == System.Data.ConnectionState.Open)
                    conn.Close();
                return NotFound();
            }


        }
        ////APP
        ///
        // GET: api/somiod
        [HttpGet]
        [Route("api/somiod")]
        public IEnumerable<Application> GetApplications()

        {

            List<Application> listApplications = new List<Application>();
            string sql = "SELECT * FROM Applications ORDER BY Id";
            SqlConnection conn = null;

            try
            {
                conn = new SqlConnection(connectionString);
                conn.Open();

                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    Application application = new Application
                    {
                        Id = (int)reader["Id"],
                        Name = (string)reader["Name"],
                        Creation_dt = (DateTime)reader["Creation_dt"]

                    };
                    listApplications.Add(application);
                }
                reader.Close();
                conn.Close();
            }

            catch (Exception)
            {

                if (conn.State == System.Data.ConnectionState.Open)
                    conn.Close();
            }

            //return new List<Application>(this.listApplications);

            return listApplications;
        }

        // GET: api/somiod/5
        [HttpGet]
        [Route("api/somiod/{id:int}")]
        public IHttpActionResult Get(int id)
        {

            string sql = "SELECT * FROM Applications WHERE Id=@id";
            SqlConnection conn = null;
            Application application = null;

            try
            {
                conn = new SqlConnection(connectionString);
                conn.Open();

                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@id", id);
                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.Read())
                {
                    application = new Application
                    {
                        Id = (int)reader["Id"],
                        Name = (string)reader["Name"],
                        Creation_dt = (DateTime)reader["Creation_dt"]

                    };

                }
                reader.Close();
                conn.Close();

                if (application == null)
                {
                    return NotFound();
                }

                return Ok(application);
            }
            catch (Exception)
            {

                if (conn.State == System.Data.ConnectionState.Open)
                    conn.Close();
                return NotFound();
            }


        }
    }
}