using System.Web.Http;

namespace RealEstateWebClient.Controllers
{
  using System;
  using System.Collections.Generic;

  using MySql.Data.MySqlClient;

  using RealEstateWebClient.Models;

  public class RealEstatesController : ApiController
    {

    public IEnumerable<RealEstateModel> GetAllRealEstates([FromUri] int from, int to)
    {
      string MyConString =
        System.Configuration.ConfigurationManager.ConnectionStrings["RealEstateDBContext"].ConnectionString;

      string sql = "select * from RealEstate limit " + from + ", " + to;

      try
      {
        var connection = new MySqlConnection(MyConString);
        var cmdSel = new MySqlCommand(sql, connection);

        connection.Open();

        MySqlDataReader dataReader = cmdSel.ExecuteReader();

        List<RealEstateModel> pom = new List<RealEstateModel>();

        while (dataReader.Read())
        {
          pom.Add(
            new RealEstateModel
              {
                Id = int.Parse(dataReader["id"].ToString()),
                Company = dataReader["company"].ToString(),
                City = dataReader["city"].ToString(),
                Location = dataReader["location"].ToString(),
                Type = dataReader["type"].ToString(),
                SquareMeters = int.Parse(dataReader["squaremeters"].ToString()),
                Price = float.Parse(dataReader["price"].ToString()),
                Link = dataReader["link"].ToString(),
                Active = int.Parse(dataReader["active"].ToString()),
                UpdateTime = dataReader["updatetime"].ToString(),
                UpdateDate = dataReader["updatedate"].ToString(),
                InsertTime = dataReader["inserttime"].ToString(),
                InsertDate = dataReader["insertdate"].ToString()
              });
        }
        return pom;

      }
      catch (Exception e)
      {
        List<RealEstateModel> error = new List<RealEstateModel>();
        error.Add(new RealEstateModel { City = e.Message });
        return error;
      }


      return null;
    }

    public IEnumerable<RealEstateModel> GetAllRealEstates()
    {
      return null;
    }

    }
}
