예제 #1
0
        public CustomerAssetsListData GetAssetsForCustomer(List <Guid> customerGuids, int pageNum, int pageLimit)
        {
            var assetsToBeReturned = new CustomerAssetsListData();

            using (var connection = new MySqlConnection(connectionString))
            {
                var assets = new List <CustomerAsset>();
                try
                {
                    int rowToStart = (pageNum - 1) * pageLimit;
                    int noOfRows   = pageLimit;

                    List <string> lstCustomerGuids = customerGuids.Select(x => x.ToStringWithoutHyphens().WrapWithUnhex()).ToList();

                    string GetAssetsForCustomerQuery =
                        $"select SQL_CALC_FOUND_ROWS hex(a.AssetUID) as AssetUID, a.AssetName, a.LegacyAssetID, a.SerialNumber, a.MakeCode, a.Model, a.AssetTypeName, a.EquipmentVIN, a.IconKey, a.ModelYear, a.StatusInd,hex(a.OwningCustomerUID) as OwningCustomerUID from md_asset_Asset a " +
                        $" inner join md_customer_CustomerAsset ca on ca.fk_AssetUID = a.AssetUID " +
                        $" where a.StatusInd=1 and ca.fk_CustomerUID in ({"{0}"}) order by a.SerialNumber  LIMIT {rowToStart}, {noOfRows};select found_rows() as TotalNumberOfRows;";

                    string query = string.Format(GetAssetsForCustomerQuery, string.Join(",", lstCustomerGuids));

                    var customerAssets = _transaction.Get <CustomerAsset>(query).ToList();
                    var totalRows      = customerAssets.Count();

                    customerAssets.All(x => { x.OwningCustomerUID = x.OwningCustomerUID != null ? (Guid?)new Guid(x.OwningCustomerUID.ToString()) : null; x.AssetUID = x.AssetUID != null ? (Guid?)new Guid(x.AssetUID.ToString()) : null; return(true); });

                    assetsToBeReturned.CustomerAssets     = customerAssets;
                    assetsToBeReturned.TotalRowsCount     = totalRows > 0 ? totalRows : 0;
                    assetsToBeReturned.TotalNumberOfPages = (int)Math.Ceiling(Convert.ToDouble(Convert.ToDouble(totalRows) / pageLimit));
                    assetsToBeReturned.PageNumber         = assetsToBeReturned.TotalNumberOfPages > 0 ? pageNum : 0;
                }
                catch (Exception ex)
                {
                    _logger.LogError($"{ex}");
                    throw;
                }
                finally
                {
                }
                return(assetsToBeReturned);
            }
        }
예제 #2
0
        public ActionResult GetAssets(
            [FromQuery] string[] assetUIDs    = null, [FromQuery] string customerUid = null,
            [FromQuery] string[] assetType    = null, [FromQuery] string[] status    = null,
            [FromQuery] string[] manufacturer = null, [FromQuery] string[] model     = null,
            [FromQuery] string snContains     = null,
            [FromQuery] string pageSize       = "10", [FromQuery] string pageNumber = "1",
            Guid?userGuid = null, Guid?accountSelectionGuid = null)
        {
            bool isIntegrator = false;

            try
            {
                int         pageSizeInt;
                int         pageNumberInt;
                Guid?       customerGuid;
                List <Guid> customerGuids = new List <Guid>()
                {
                };

                _logger.LogDebug("Before JWT Call.Requestheaders:" + String.Join(",", Request.Headers.Select(x => x.Key + ":" + x.Value)));

                if (!Request.Headers.TryGetValue("X-JWT-Assertion", out StringValues headerValues))
                {
                    return(BadRequest("Could not validate X-VisionLink-UserUid or X-JWT-Assertion Headers in Request"));
                }

                TPaaSJWT jwt;
                try
                {
                    jwt = new TPaaSJWT(headerValues);
                }
                catch
                {
                    jwt = null;
                }


                if (jwt != null)
                {
                    if (userGuid == null || !userGuid.HasValue)
                    {
                        userGuid = jwt.UserUid;
                    }

                    _logger.LogDebug(string.Format("ApplicationName:{0},uuid:{1},encodedjwt:{2}", jwt.ApplicationName, jwt.UserUid, jwt.EncodedJWT));

                    if (!string.IsNullOrEmpty(jwt.ApplicationName))
                    {
                        _logger.LogInformation("JWT has an appname " + jwt.ApplicationName);
                        var customers = _assetService.GetCustomersForApplication(jwt.ApplicationName);                         // new List<Guid>() { new Guid("07D4A55244C5E311AA7700505688274D") };//

                        if (customers != null && customers.Count > 0)
                        {
                            _logger.LogInformation("CustomerCount>0" + new Guid(customers[0].ToString()));
                            customerGuid = !string.IsNullOrEmpty(customerUid) ? new Guid(customerUid) : (Guid?)null;
                            if (customerGuid.HasValue)
                            {
                                if (customers.Contains((Guid)customerGuid))
                                {
                                    customerGuids.Add((Guid)customerGuid);
                                    isIntegrator = true;
                                }
                                else
                                {
                                    return(BadRequest("Application does not have this customer mapped. Please contact your API administrator."));
                                }
                            }
                            else
                            {
                                customerGuids = customers;
                                isIntegrator  = true;
                            }
                        }
                        if (isIntegrator)
                        {
                            try
                            {
                                _controllerUtilities.ValidatePageParameters(pageSize, pageNumber, out pageSizeInt, out pageNumberInt);
                            }
                            catch (Exception ex)
                            {
                                _logger.LogError("Get Assets parameter validation threw an exception", ex);
                                return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message));                               //BadRequest("Assets parameter validation failed.");
                            }
                            if (customerGuids != null && customerGuids.Any())
                            {
                                CustomerAssetsListData assets = _assetService.GetAssetsForCustomer(customerGuids, pageNumberInt, pageSizeInt);
                                return(Ok(assets));
                            }
                        }
                        else
                        {
                            if (!userGuid.HasValue)
                            {
                                return(BadRequest("UserUID has not been provided"));
                            }

                            Guid[]          assetGuids;
                            WildcardMatches wildcardMatches;
                            try
                            {
                                assetGuids      = _controllerUtilities.ValidateAssetUIDParameters(assetUIDs);
                                wildcardMatches = ValidateWildcardMatches(null, snContains);
                                _controllerUtilities.ValidatePageParameters(pageSize, pageNumber, out pageSizeInt, out pageNumberInt);
                                customerGuid = !string.IsNullOrEmpty(customerUid) ? new Guid(customerUid) : (Guid?)null;
                            }
                            catch (Exception ex)
                            {
                                _logger.LogError("Get Assets parameter validation threw an exception", ex);
                                return(BadRequest(" Assets parameter validation " + ex.Message));                                //StatusCode((int)HttpStatusCode.InternalServerError, ex.Message);//
                            }

                            if (!assetGuids.Any())
                            {
                                assetGuids = GetAssetUIDsFromFilters(
                                    userGuid.Value, accountSelectionGuid, customerGuid, assetType, status, manufacturer, model,
                                    wildcardMatches, pageSizeInt, pageNumberInt);
                            }

                            if (assetGuids != null && assetGuids.Any())
                            {
                                var assets = GetAssetsFromAssetGuids(assetGuids, userGuid.Value);
                                return(Ok(assets));
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.LogError("Get AssetLists encountered an error", ex);
                return(StatusCode((int)HttpStatusCode.InternalServerError, ex.Message));
            }
            return(Ok(isIntegrator ? (object)new List <DataModels.CustomerAsset>() : (object)new List <DataModels.Asset>()));
        }