Example #1
0
        public static CarForRentModel ToObject(DataRow reader)
        {
            DateTime dateValue;

            CarForRentModel carForRentModel = new CarForRentModel();

            carForRentModel.rentStartDate = DateTime.Parse(reader[0].ToString());
            carForRentModel.rentEndDate   = DateTime.Parse(reader[1].ToString());


            if (DateTime.TryParse(reader[2].ToString(), out dateValue))
            {
                carForRentModel.rentRealEndDate = DateTime.Parse(reader[2].ToString());
            }
            else
            {
                carForRentModel.rentRealEndDate = null;
            }

            carForRentModel.userID     = reader[3].ToString();
            carForRentModel.carNumber  = reader[4].ToString();
            carForRentModel.rentNumber = int.Parse(reader[5].ToString());

            Debug.WriteLine("carForRentModel: " + carForRentModel.ToString());
            return(carForRentModel);
        }
 static public SqlCommand UpdateCarForRent(CarForRentModel carForRentModel)
 {
     if (GlobalVariable.queryType == 0)
     {
         return(CreateSqlCommand(carForRentModel, queryCarForRentUpdate));
     }
     else
     {
         return(CreateSqlCommand(carForRentModel, procedureCarForRentUpdate));
     }
 }
        public CarForRentModel AddCarForRent(CarForRentModel carForRentModel)
        {
            if (carForRentModel.rentRealEndDate == null)
            {
                carForRentModel.rentRealEndDate = carForRentModel.rentStartDate.AddDays(-36);
            }

            _carsForRent.InsertOne(carForRentModel);
            CarForRentModel tmpCarForRentModel = GetOneCarForRentByRentNumber(carForRentModel.rentNumber);

            return(tmpCarForRentModel);
        }
        public CarForRentModel UpdateCarForRent(CarForRentModel carForRentModel)
        {
            if (carForRentModel.rentRealEndDate == null)
            {
                carForRentModel.rentRealEndDate = carForRentModel.rentStartDate.AddDays(-36);
            }

            _carsForRent.ReplaceOne(carsForRent => carsForRent.rentNumber == carForRentModel.rentNumber, carForRentModel);
            CarForRentModel tmpCarForRentModel = GetOneCarForRentByRentNumber(carForRentModel.rentNumber);

            return(tmpCarForRentModel);
        }
        static private SqlCommand CreateSqlCommand(CarForRentModel carForRent, string commandText)
        {
            SqlCommand command = new SqlCommand(commandText);

            command.Parameters.AddWithValue("@rentNumber", carForRent.rentNumber);
            command.Parameters.AddWithValue("@userID", carForRent.userID);
            command.Parameters.AddWithValue("@carNumber", carForRent.carNumber);
            command.Parameters.AddWithValue("@rentStartDate", carForRent.rentStartDate);
            command.Parameters.AddWithValue("@rentEndDate", carForRent.rentEndDate);
            command.Parameters.AddWithValue("@rentRealEndDate", carForRent.rentRealEndDate.HasValue ? carForRent.rentRealEndDate: carForRent.rentStartDate.AddDays(-36));

            return(command);
        }
        public List <CarForRentModel> GetAllCarsForRent()
        {
            DataTable dt = new DataTable();
            List <CarForRentModel> arrCarsForRent = new List <CarForRentModel>();

            using (MySqlCommand command = new MySqlCommand())
            {
                dt = GetMultipleQuery(CarForRentStringsMySql.GetAllCarsForRent());
            }

            foreach (DataRow ms in dt.Rows)
            {
                arrCarsForRent.Add(CarForRentModel.ToObject(ms));
            }

            return(arrCarsForRent);
        }
 //[BasicAuthentication]
 //[AutorizeByRole(AutorizeByRoleAttribute.roles.Admin)]
 public HttpResponseMessage AddCarForRent(CarForRentModel carForRentModel, string userName)
 {
     try
     {
         HttpResponseMessage hrm = new HttpResponseMessage(HttpStatusCode.Created)
         {
             Content = new StringContent(JsonConvert.SerializeObject(carForRentRepository.AddCarForRent(carForRentModel)))
         };
         return(hrm);
     }
     catch (Exception ex)
     {
         Errors errors          = ErrorsHelper.GetErrors(ex);
         HttpResponseMessage hr = new HttpResponseMessage(HttpStatusCode.InternalServerError)
         {
             Content = new StringContent(errors.ToString())
         };
         return(hr);
     }
 }
        public CarForRentModel AddCarForRent(CarForRentModel carForRentModel)
        {
            string id = HttpContext.Current.User.Identity.Name;

            carForRentModel.userID = id;
            DataTable       dt = new DataTable();
            CarForRentModel carForRentModelSql = new CarForRentModel();

            using (MySqlCommand command = new MySqlCommand())
            {
                dt = GetMultipleQuery(CarForRentStringsMySql.AddCarForRent(carForRentModel));
            }

            foreach (DataRow ms in dt.Rows)
            {
                carForRentModelSql = CarForRentModel.ToObject(ms);
            }

            return(carForRentModelSql);
        }
 //[BasicAuthentication]
 //[AutorizeByRole(AutorizeByRoleAttribute.roles.Manager)]
 public HttpResponseMessage UpdateCarForRent(int updateByRentNumber, CarForRentModel carForRentModel)
 {
     try
     {
         carForRentModel.rentNumber = updateByRentNumber;
         HttpResponseMessage hrm = new HttpResponseMessage(HttpStatusCode.OK)
         {
             Content = new StringContent(JsonConvert.SerializeObject(carForRentRepository.UpdateCarForRent(carForRentModel)))
         };
         return(hrm);
     }
     catch (Exception ex)
     {
         Errors errors          = ErrorsHelper.GetErrors(ex);
         HttpResponseMessage hr = new HttpResponseMessage(HttpStatusCode.InternalServerError)
         {
             Content = new StringContent(errors.ToString())
         };
         return(hr);
     }
 }
        public CarForRentModel GetOneCarForRentByRentNumber(int rentNumber)
        {
            if (rentNumber < 0)
            {
                throw new ArgumentOutOfRangeException();
            }
            DataTable       dt = new DataTable();
            CarForRentModel carForRentModelSql = new CarForRentModel();

            using (MySqlCommand command = new MySqlCommand())
            {
                dt = GetMultipleQuery(CarForRentStringsMySql.GetOneCarForRentByRentNumber(rentNumber));
            }

            foreach (DataRow ms in dt.Rows)
            {
                carForRentModelSql = CarForRentModel.ToObject(ms);
            }

            return(carForRentModelSql);
        }
        public List <CarForRentModel> GetCarsForRentByCarNumber(string carNumber)
        {
            if (carNumber.Equals(""))
            {
                throw new ArgumentOutOfRangeException();
            }
            DataTable dt = new DataTable();
            List <CarForRentModel> arrCarsForRent = new List <CarForRentModel>();

            using (MySqlCommand command = new MySqlCommand())
            {
                dt = GetMultipleQuery(CarForRentStringsMySql.GetAllCarsForRentByCarNumber(carNumber));
            }

            foreach (DataRow ms in dt.Rows)
            {
                arrCarsForRent.Add(CarForRentModel.ToObject(ms));
            }

            return(arrCarsForRent);
        }
        public CarForRentModel UpdateCarForRent(CarForRentModel carForRentModel)
        {
            string id = HttpContext.Current.User.Identity.Name;

            carForRentModel.userID = id;

            if (carForRentModel.rentRealEndDate == null)
            {
                carForRentModel.rentRealEndDate = carForRentModel.rentStartDate.AddDays(-36);
            }

            if (GlobalVariable.queryType == 0)
            {
                CARFORRENT carForRent = DB.CARFORRENTS.Where(cfr => cfr.rentNumber == carForRentModel.rentNumber).SingleOrDefault();
                if (carForRent == null)
                {
                    return(null);
                }
                carForRent.carNumber       = carForRentModel.carNumber;
                carForRent.userID          = carForRentModel.userID;
                carForRent.rentStartDate   = carForRentModel.rentStartDate;
                carForRent.rentEndDate     = carForRentModel.rentEndDate;
                carForRent.rentRealEndDate = carForRentModel.rentRealEndDate;
                DB.SaveChanges();
                return(GetOneCarForRentByRentNumber(carForRent.rentNumber));
            }
            else
            {
                return(DB.UpdateCarForRent(carForRentModel.rentStartDate, carForRentModel.rentEndDate, carForRentModel.rentRealEndDate, carForRentModel.userID, carForRentModel.carNumber, carForRentModel.rentNumber).Select(cfr => new CarForRentModel
                {
                    rentNumber = cfr.rentNumber,
                    carNumber = cfr.carNumber,
                    userID = cfr.userID,
                    rentStartDate = cfr.rentStartDate,
                    rentEndDate = cfr.rentEndDate,
                    rentRealEndDate = cfr.rentRealEndDate
                }).SingleOrDefault());
            }
        }
 public HttpResponseMessage AddCarForRent(CarForRentModel carForRentModel)
 {
     try
     {
         if (carForRentModel == null)
         {
             return(Request.CreateResponse(HttpStatusCode.BadRequest, "Data is null."));
         }
         if (!ModelState.IsValid)
         {
             Errors errors = ErrorsHelper.GetErrors(ModelState);
             return(Request.CreateResponse(HttpStatusCode.BadRequest, errors));
         }
         CarForRentModel addedCarForRent = carForRentRepository.AddCarForRent(carForRentModel);
         return(Request.CreateResponse(HttpStatusCode.Created, addedCarForRent));
     }
     catch (Exception ex)
     {
         Errors errors = ErrorsHelper.GetErrors(ex);
         return(Request.CreateResponse(HttpStatusCode.InternalServerError, errors));
     }
 }
        public HttpResponseMessage UpdateForRent(int number, CarForRentModel carForRentModel)
        {
            try
            {
                if (carForRentModel == null)
                {
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, "Data is null."));
                }
                if (!ModelState.IsValid)
                {
                    Errors errors = ErrorsHelper.GetErrors(ModelState);
                    return(Request.CreateResponse(HttpStatusCode.BadRequest, errors));
                }

                carForRentModel.rentNumber = number;
                CarForRentModel updatedCarForRent = carForRentRepository.UpdateCarForRent(carForRentModel);
                return(Request.CreateResponse(HttpStatusCode.OK, updatedCarForRent));
            }
            catch (Exception ex)
            {
                Errors errors = ErrorsHelper.GetErrors(ex);
                return(Request.CreateResponse(HttpStatusCode.InternalServerError, errors));
            }
        }