コード例 #1
0
 public IActionResult GetVehicleById(int id)
 {
     try {
         var vehicle = _vehicleLogic.GetVehicleById(id);
         if (vehicle == null)
         {
             return(BadRequest());
         }
         var requestHeaders = HttpContext.Request.Headers;
         if (requestHeaders.TryGetValue("Authorization", out StringValues token))
         {
             var userRole = JwtService.GetRoleFromToken(JwtService.DecodeToken(token.ToString()));
             if (userRole == Roles.user.GetEnumDescription() &&
                 (vehicle.IsActive == null ||
                  !(bool)vehicle.IsActive ||
                  !vehicle.IsAvailable ||
                  !vehicle.IsProper)
                 )
             {
                 return(BadRequest());
             }
         }
         else
         {
             return(Unauthorized());
         }
         return(Ok(vehicle));
     } catch (Exception e) {
         return(StatusCode(500, e));
     }
 }
コード例 #2
0
 public IActionResult AddOrder([FromBody] NewOrderModel model)
 {
     try {
         if (model == null)
         {
             return(BadRequest("Body content is not valid!"));
         }
         if (!ModelState.IsValid)
         {
             return(BadRequest(ModelState));
         }
         if (!(model.StartDate.ToLocalTime() > DateTime.Now.AddDays(-1)) ||
             !(model.EndDate > model.StartDate)
             )
         {
             return(BadRequest("Inavalid dates range!"));
         }
         var requestHeaders = HttpContext.Request.Headers;
         int userId         = 0;
         if (requestHeaders.TryGetValue("Authorization", out StringValues token))
         {
             userId = JwtService.GetUserIdFromToken(JwtService.DecodeToken(token.ToString()));
             if (userId < 1)
             {
                 return(Unauthorized());
             }
         }
         else
         {
             return(Unauthorized());
         }
         var entity = model.Map <OrderEntity>();
         entity.StartDate = entity.StartDate.ToLocalTime();
         entity.EndDate   = entity.EndDate.ToLocalTime();
         entity.UserId    = userId;
         var vehicle = _vehicleLogic.GetVehicleById(entity.VehicleId);
         if (vehicle == null || vehicle.CarClass == null)
         {
             return(BadRequest());
         }
         var userRole = JwtService.GetRoleFromToken(JwtService.DecodeToken(token.ToString()));
         if (userRole == Roles.user.GetEnumDescription() &&
             (vehicle.IsActive == null ||
              !(bool)vehicle.IsActive ||
              !vehicle.IsAvailable ||
              !vehicle.IsProper)
             )
         {
             return(BadRequest());
         }
         entity.Price = (decimal)((entity.EndDate - entity.StartDate).TotalDays * vehicle.CarClass.DailyPrice);
         entity.Price = decimal.Round(entity.Price, 2);
         entity       = _orderLogic.AddOrder(entity);
         if (entity == null)
         {
             throw new Exception("Somthing went wrong while adding model to DB!");
         }
         if (entity.User != null)
         {
             entity.User.Password = "";
         }
         return(Ok(entity));
     } catch (Exception e) {
         return(StatusCode(500, e));
     }
 }