Beispiel #1
0
        /// <summary>
        /// Get all inventories by product id
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="tableName"></param>
        /// <param name="context"></param>
        /// <param name="trackingGuid"></param>
        /// <returns></returns>
        public async Task <List <Tagge.Common.Models.InventoryResponse> > GetAll(long productId, string tableName, Guid trackingGuid)
        {
            // Response structure
            var response = new List <Tagge.Common.Models.InventoryResponse>();

            // Company Id
            var companyId = context.Security.GetCompanyId();

            // MongoDB Settings
            var    database       = context.MongoDbSettings.Value.Databases.FirstOrDefault(x => x.Name == "DeathStar");
            string collectionName = database.Collections.FirstOrDefault(x => x.Name == "PC_Inventory").Name;

            // Get MongoDB
            var db = context.Database;
            var inventoryCollection = db.GetCollection <Deathstar.Data.Models.PC_Inventory>(collectionName);

            // Filter
            var filters = Builders <Deathstar.Data.Models.PC_Inventory> .Filter.Eq(x => x.DV_CompanyId, companyId.ToString());

            filters = filters & Builders <Deathstar.Data.Models.PC_Inventory> .Filter.Eq(x => x.InternalId, productId.ToString());

            filters = filters & Builders <Deathstar.Data.Models.PC_Inventory> .Filter.Eq(x => x.ST_TableName, tableName);

            filters = filters & Builders <Deathstar.Data.Models.PC_Inventory> .Filter.Eq(x => x.IsActive, true);

            var dbInventories = await inventoryCollection.Find(filters).ToListAsync();

            // Build the Response
            foreach (var dbInventory in dbInventories)
            {
                var singleResponse = new Tagge.Common.Models.InventoryResponse();
                singleResponse = dbInventory.ConvertToResponse();

                // Inventory Table
                string inventoryTable = string.Empty;

                // Set inventory table
                if (dbInventory.ST_TableName == "PC_Product")
                {
                    inventoryTable = "PC_ProductInventory";
                }
                else
                {
                    inventoryTable = "PC_ProductVariantInventory";
                }

                // External Ids
                singleResponse.ExternalIds = await _externalIdModel.GetByParentId(dbInventory.Id.ToString(), inventoryTable, trackingGuid);

                response.Add(singleResponse);
            }

            return(response);
        }
Beispiel #2
0
        /// <summary>
        /// Get a single product by Id
        /// </summary>
        /// <param name="id">product id</param>
        /// <param name="context">Database context see ContextModel.cs</param>
        /// <param name="trackingGuid"></param>
        /// <returns></returns>
        public async Task <Tagge.Common.Models.ProductResponse> GetProductById(long id, Guid trackingGuid)
        {
            var companyId = context.Security.GetCompanyId();

            // MongoDB Settings
            var    database       = context.MongoDbSettings.Value.Databases.FirstOrDefault(x => x.Name == "DeathStar");
            string collectionName = database.Collections.FirstOrDefault(x => x.Name == "PC_Product").Name;

            // Get MongoDB
            var db = context.Database;
            var productCollection = db.GetCollection <Deathstar.Data.Models.PC_Product>(collectionName);

            // Filters - note always start with the company id
            var filters = Builders <Deathstar.Data.Models.PC_Product> .Filter.Eq(x => x.DV_CompanyId, companyId.ToString());

            filters = filters & Builders <Deathstar.Data.Models.PC_Product> .Filter.Eq(x => x.Id, id);

            filters = filters & Builders <Deathstar.Data.Models.PC_Product> .Filter.Eq(x => x.IsActive, true);

            var dbProduct = await productCollection.FindAsync(filters).Result.FirstOrDefaultAsync();

            if (dbProduct == null)
            {
                string reason = $"Product not found by Id:{id} provided.";

                IG2000.Data.Utilities.Logging.LogTrackingEvent($"Product was unable to be retrieved! Reason: {reason}", $"Status Code: {Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound}", LT319.Common.Utilities.Constants.TrackingStatus.Error, context, trackingGuid);
                throw new HttpResponseException()
                      {
                          StatusCode = Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound, ReasonPhrase = reason
                      };
            }

            //IG2000.Data.Utilities.Logging.LogTrackingEvent($"Product (id: {dbProduct.Id}, sku: {dbProduct.Sku}) successfully retrieved.", "Get Product (MongoDB)", LT319.Common.Utilities.Constants.TrackingStatus.Complete, trackingGuid);

            // Build the Response
            var response = dbProduct.ConvertToResponse(companyId.ToString(), context.Database);

            // Inventory
            response.Inventory = await _inventoryModel.GetById(dbProduct.Id.ToString(), "PC_Product", trackingGuid);

            // Variant
            response.Variants = await _variantModel.GetAll(dbProduct.Id, trackingGuid);

            // External Ids
            response.ExternalIds = await _externalIdModel.GetByParentId(dbProduct.Id.ToString(), "PC_Product", trackingGuid);

            return(response);
        }
Beispiel #3
0
        /// <summary>
        /// Get an existing category by its id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="context"></param>
        /// <param name="trackingGuid"></param>
        /// <returns></returns>
        public async Task <Tagge.Common.Models.CategoryResponse> GetById(IBaseContextModel context, long id, Guid trackingGuid)
        {
            // Company Id
            var companyId = context.Security.GetCompanyId();

            //// MongoDB Settings
            //var database = context.MongoDbSettings.Value.Databases.FirstOrDefault(x => x.Name == "DeathStar");
            //string collectionName = database.Collections.FirstOrDefault(x => x.Name == "PC_Category").Name;

            //// Get MongoDB
            //var db = context.Database;
            //var categoryCollection = db.GetCollection<Deathstar.Data.Models.PC_Category>(collectionName);

            // Filter
            var filters = Builders <Deathstar.Data.Models.PC_Category> .Filter.Eq(x => x.Id, id);

            filters = filters & Builders <Deathstar.Data.Models.PC_Category> .Filter.Eq(x => x.DV_CompanyId, companyId.ToString());

            var dbCategory = await _categoryCollection.FindAsync(filters).Result.FirstOrDefaultAsync();

            if (dbCategory == null)
            {
                string reason = $"Category not found by Id:{id} provided.";

                IG2000.Data.Utilities.Logging.LogTrackingEvent($"Category was unable to be retrieved! Reason: {reason}", $"Status Code: {Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound}", LT319.Common.Utilities.Constants.TrackingStatus.Error, context, trackingGuid);
                throw new HttpResponseException()
                      {
                          StatusCode = Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound, ReasonPhrase = reason
                      };
            }

            IG2000.Data.Utilities.Logging.LogTrackingEvent($"Category (id: {dbCategory.Id}, name: {dbCategory.Name}) successfully retrieved.", "Get Category", LT319.Common.Utilities.Constants.TrackingStatus.Complete, context, trackingGuid);

            // Build the Response
            var response = dbCategory.ConvertToResponse();

            // Add Id
            response.Id = dbCategory.Id;

            // External Ids
            response.ExternalIds = await _externalIdModel.GetByParentId(dbCategory.Id.ToString(), "PC_Category", trackingGuid);

            return(response);
        }
Beispiel #4
0
        /// <summary>
        /// Get a kit by id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="context"></param>
        /// <param name="trackingGuid"></param>
        /// <returns></returns>
        public async Task <Tagge.Common.Models.KitResponse> GetById(string id, Guid trackingGuid)
        {
            var response  = new Tagge.Common.Models.KitResponse();
            var companyId = context.Security.GetCompanyId();

            // MongoDB Settings
            var    database              = context.MongoDbSettings.Value.Databases.FirstOrDefault(x => x.Name == "DeathStar");
            string collectionName        = database.Collections.FirstOrDefault(x => x.Name == "PC_Product").Name;
            string variantCollectionName = database.Collections.FirstOrDefault(x => x.Name == "PC_ProductVariant").Name;

            // Get MongoDB
            var db = context.Database;
            var productCollection = db.GetCollection <Deathstar.Data.Models.PC_Product>(collectionName);
            var variantCollection = db.GetCollection <Deathstar.Data.Models.PC_ProductVariant>(variantCollectionName);

            // Break combined id
            string[] ids = id.Split('|');

            // Parent id
            long.TryParse(ids[0], out long parentId);

            // Filters - note always start with the company id
            var filters = Builders <Deathstar.Data.Models.PC_Product> .Filter.Eq(x => x.DV_CompanyId, companyId.ToString());

            filters = filters & Builders <Deathstar.Data.Models.PC_Product> .Filter.Eq(x => x.Id, parentId);

            filters = filters & Builders <Deathstar.Data.Models.PC_Product> .Filter.Eq(x => x.IsActive, true);

            // Does the product exist?
            var dbProduct = await productCollection.FindAsync(filters).Result.FirstOrDefaultAsync();

            // Looks like it does
            if (dbProduct != null)
            {
                // Build the Product version Response
                response = dbProduct.Kit.ConvertToKitResponse();

                // Add Id
                response.Id = dbProduct.Kit.PC_Kit_Id;

                // External Ids
                response.ExternalIds = await _externalIdModel.GetByParentId(dbProduct.Kit.PC_Kit_Id.ToString(), "PC_Kit", trackingGuid);
            }
            else
            {
                // Product wasnt found now gotta try the variant
                var variantFilters = Builders <Deathstar.Data.Models.PC_ProductVariant> .Filter.Eq(x => x.DV_CompanyId, companyId.ToString());

                variantFilters = variantFilters & Builders <Deathstar.Data.Models.PC_ProductVariant> .Filter.Eq(x => x.Id, parentId);

                variantFilters = variantFilters & Builders <Deathstar.Data.Models.PC_ProductVariant> .Filter.Eq(x => x.IsActive, true);

                // Where in the world is the variant?
                var dbVariant = await variantCollection.FindAsync(variantFilters).Result.FirstOrDefaultAsync();

                // Its missing of course!
                if (dbVariant == null)
                {
                    string reason = $"Kit not found by Id:{id} provided.";

                    IG2000.Data.Utilities.Logging.LogTrackingEvent($"Kit was unable to be retrieved! Reason: {reason}", $"Status Code: {Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound}", LT319.Common.Utilities.Constants.TrackingStatus.Error, context, trackingGuid);
                    throw new HttpResponseException()
                          {
                              StatusCode = Microsoft.AspNetCore.Http.StatusCodes.Status404NotFound, ReasonPhrase = reason
                          };
                }

                // Build the Variant version Response
                response = dbVariant.Kit.ConvertToKitResponse();

                // Add Id
                response.Id = dbVariant.Kit.PC_Kit_Id;

                // External Ids
                response.ExternalIds = await _externalIdModel.GetByParentId(dbVariant.Kit.PC_Kit_Id.ToString(), "PC_VariantKit", trackingGuid);
            }

            //IG2000.Data.Utilities.Logging.LogTrackingEvent($"Product (id: {dbProduct.Id}, sku: {dbProduct.Sku}) successfully retrieved.", "Get Product (MongoDB)", LT319.Common.Utilities.Constants.TrackingStatus.Complete, trackingGuid);

            return(response);
        }