public HttpResponseMessage PutOrder([FromBody] RentalOrderModel orderModel)
 {
     try
     {
         var errorMessage = string.Empty;
         if (ModelState.IsValid)
         {
             if (rentsAndOrdersManager.UpdateOrder(orderModel, out errorMessage))
             {
                 return(Request.CreateResponse(HttpStatusCode.OK, true));
             }
             return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, errorMessage));
         }
         else
         {
             errorMessage = ModelState.Values.SelectMany(v => v.Errors).ToList().Select(e => e.ErrorMessage).Where(e => e != "").FirstOrDefault();
             if (errorMessage == null)
             {
                 errorMessage = ModelState.Values.SelectMany(v => v.Errors).ToList().Select(e => e.Exception).Where(e => e.Message != "").Select(e => e.Message).FirstOrDefault();
             }
             if (errorMessage != null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, errorMessage));
             }
         }
         return(Request.CreateErrorResponse(HttpStatusCode.NotFound, new HttpError()));
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
     }
 }
 public HttpResponseMessage Post([FromBody] RentalOrderModel order)
 {
     try
     {
         var userName     = RequestContext.Principal.Identity.Name;
         var errorMessage = string.Empty;
         if (ModelState.IsValid)
         {
             int newOrderId = rentsAndOrdersManager.AddNewOrder(userName, order, out errorMessage);
             if (newOrderId > 0)
             {
                 order.OrderId = newOrderId;
                 return(Request.CreateResponse(HttpStatusCode.OK, order));
             }
             return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, errorMessage));
         }
         else
         {
             errorMessage = ModelState.Values.SelectMany(v => v.Errors).ToList().Select(e => e.ErrorMessage).Where(e => e != "").FirstOrDefault();
             if (errorMessage == null)
             {
                 errorMessage = ModelState.Values.SelectMany(v => v.Errors).ToList().Select(e => e.Exception).Where(e => e.Message != "").Select(e => e.Message).FirstOrDefault();
             }
             if (errorMessage != null)
             {
                 return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, errorMessage));
             }
         }
         return(Request.CreateErrorResponse(HttpStatusCode.NotFound, new HttpError("Problem in order data")));
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
     }
 }
Esempio n. 3
0
        public int AddNewOrder(string userName, RentalOrderModel order, out string errorMessage)
        {
            errorMessage = "";
            try
            {
                if (order.StartRent.Date < DateTime.Now.Date)
                {
                    errorMessage = $"The order start date {order.StartRent.Date} should be today {DateTime.Now.Date} or later";
                    return(-3);
                }
                if (order.EndRent.Date <= order.StartRent.Date)
                {
                    errorMessage = "The order end date should be greater than the start date";
                    return(-3);
                }
                using (CarRentalEntities carEntities = new CarRentalEntities())
                {
                    Car car = carEntities.Cars.Where(c => c.LicensePlate == order.CarToRent.LicensePlate).FirstOrDefault();
                    if (car == null)
                    {
                        errorMessage = "Car not found";
                        return(-1);
                    }
                    User user = carEntities.Users.Where(u => u.UserName == order.RentingUser.UserName).FirstOrDefault();
                    if (user == null)
                    {
                        errorMessage = "User not found";
                        return(-1);
                    }
                    RentalOrder newOrder = new RentalOrder()
                    {
                        CarId             = car.CarId,
                        UserId            = user.UserId,
                        RentStartDate     = order.StartRent,
                        RentEndDate       = order.EndRent,
                        ActualRentEndDate = order.ActualEndRent
                    };

                    //In a real-life application, the following should be done using a transaction, or with some locking mechanism!
                    foreach (var o in carEntities.RentalOrders)
                    {
                        if (clash(o, newOrder))
                        {
                            errorMessage = "Sorry, the car you selected is not available on these dates. Please change the dates or select another car";
                            return(-2);
                        }
                    }

                    carEntities.RentalOrders.Add(newOrder);
                    carEntities.SaveChanges();
                    return(newOrder.RentalOrderId);
                }
            }
            catch (Exception e)
            {
                return(-1);
            }
        }
Esempio n. 4
0
        public bool UpdateOrder(RentalOrderModel updatedOrder, out string errorMessage)
        {
            try
            {
                errorMessage = "";

                if (updatedOrder.EndRent.Date <= updatedOrder.StartRent.Date)
                {
                    errorMessage = $"The order end date {updatedOrder.EndRent.Date} should be greater than the start date {updatedOrder.StartRent.Date}";
                    return(false);
                }
                using (CarRentalEntities carEntities = new CarRentalEntities())
                {
                    Car car = carEntities.Cars.Where(c => c.LicensePlate == updatedOrder.CarToRent.LicensePlate).FirstOrDefault();
                    if (car == null)
                    {
                        errorMessage = $"Car {updatedOrder.CarToRent.LicensePlate} was not found";
                        return(false);
                    }
                    User user = carEntities.Users.Where(u => u.UserName == updatedOrder.RentingUser.UserName).FirstOrDefault();
                    if (user == null)
                    {
                        errorMessage = $"Renting user {updatedOrder.RentingUser.UserName} ({updatedOrder.RentingUser.FullName}) was not found";
                        return(false);
                    }

                    RentalOrder order = carEntities.RentalOrders.Where(o => o.RentalOrderId == updatedOrder.OrderId).FirstOrDefault();
                    if (order == null)
                    {
                        errorMessage = $"Order number {updatedOrder.OrderId} was not found";
                        return(false);
                    }
                    order.RentStartDate     = updatedOrder.StartRent;
                    order.RentEndDate       = updatedOrder.EndRent;
                    order.ActualRentEndDate = updatedOrder.ActualEndRent;
                    order.UserId            = user.UserId;
                    order.CarId             = car.CarId;
                    //In a real-life application, the following should be done using a transaction, or with some locking mechanism!
                    foreach (var o in carEntities.RentalOrders)
                    {
                        if (clash(o, order))
                        {
                            errorMessage = "Sorry, the car you selected is not available on these dates. Please change the dates or select another car";
                            return(false);
                        }
                    }

                    carEntities.SaveChanges();
                }
                return(true);
            }
            catch (Exception e)
            {
                errorMessage = e.Message;
                return(false);
            }
        }
 public HttpResponseMessage DeleteOrder([FromUri] RentalOrderModel order)
 {
     try
     {
         if (rentsAndOrdersManager.DeleteOrder(order))
         {
             return(Request.CreateResponse(HttpStatusCode.OK, true));
         }
         return(Request.CreateErrorResponse(HttpStatusCode.NotFound, new HttpError()));
     }
     catch (Exception ex)
     {
         return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.Message));
     }
 }
Esempio n. 6
0
 public bool DeleteOrder(RentalOrderModel updatedOrder)
 {
     try
     {
         using (CarRentalEntities carEntities = new CarRentalEntities())
         {
             RentalOrder order = carEntities.RentalOrders.Where(o => o.RentalOrderId == updatedOrder.OrderId).FirstOrDefault();
             if (order == null)
             {
                 throw new ArgumentException($"Order number {updatedOrder.OrderId} was not found");
             }
             carEntities.RentalOrders.Remove(order);
             carEntities.SaveChanges();
         }
         return(true);
     }
     catch
     {
         return(false);
     }
 }