Example #1
0
        /// <summary>
        /// Converts the request to a database object
        /// </summary>
        /// <param name="companyId"></param>
        /// <param name="request"></param>
        public void ConvertToDatabaseObject(string companyId, Tagge.Common.Models.ProductRequest request)
        {
            // Properties
            DV_CompanyId     = companyId;
            Name             = request.Name;
            Sku              = request.Sku;
            Description      = request.Description;
            DefaultPrice     = request.DefaultPrice == null ? 0 : (decimal)request.DefaultPrice;
            MSRP             = request.MSRP;
            SalePrice        = request.SalePrice;
            TaxClass         = request.TaxClass;
            Barcode          = request.Barcode;
            Unit             = request.Unit;
            AllowDiscounts   = request.AllowDiscounts;
            AllowBackorders  = request.AllowBackorders;
            InStockThreshold = request.InStockThreshold;
            TrackingMethod   = request.TrackingMethod;
            Type             = request.Type;
            Status           = request.Status;
            Width            = request.Width;
            Height           = request.Height;
            Depth            = request.Depth;
            Weight           = request.Weight;
            IsActive         = true;

            // Kit
            if (request.Kit != null)
            {
                Kit.ConvertToDatabaseObject(request.Kit);
            }

            // Variants - Managed in PC_ProductVariant

            // Inventory - Managed in PC_Inventory

            // Categories - Managed in PC_Category

            // Options
            if (request.Options != null)
            {
                foreach (var option in request.Options)
                {
                    var dbOption = new PC_Option();
                    dbOption.ConvertToDatabaseObject(option);
                    Options.Add(dbOption);
                }
            }

            // Units
            if (request.Units != null)
            {
                foreach (var unit in request.Units)
                {
                    var dbUnit = new PC_ProductUnit();
                    dbUnit.ConvertToDatabaseObject(unit);
                    Units.Add(dbUnit);
                }
            }

            // AlternateIds
            if (request.AlternateIds != null)
            {
                foreach (var alternateId in request.AlternateIds)
                {
                    var dbAlternateId = new PC_AlternateId();
                    dbAlternateId.ConvertToDatabaseObject(alternateId);
                    AlternateIds.Add(dbAlternateId);
                }
            }

            // Custom Fields - Managed in PC_CustomField

            // ExternalIds - Managed in PC_ExternalId
        }
Example #2
0
        /// <summary>
        /// Internal Use Only! Save/Update a list of options
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="options"></param>
        /// <param name="context"></param>
        /// <param name="trackingGuid"></param>
        /// <returns></returns>
        public async Task <List <Deathstar.Data.Models.PC_Option> > Save(long productId, List <Tagge.Common.Models.OptionRequest> options, Guid trackingGuid)
        {
            var dbOptions = new List <Deathstar.Data.Models.PC_Option>();

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

            var productCollection = context.Database.GetCollection <Deathstar.Data.Models.PC_Product>("PC_Product");

            // 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, productId);

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

            try
            {
                if (options != null && options.Count > 0)
                {
                    foreach (var option in options)
                    {
                        var dbOption = new Deathstar.Data.Models.PC_Option();

                        if (dbProduct != null && dbProduct.Options != null)
                        {
                            dbOption = dbProduct.Options.FirstOrDefault(x => x.Name.ToLower() == option.OptionName.ToLower() && x.IsActive);
                        }

                        if (dbProduct == null || dbProduct.Options == null || dbOption == null)
                        {
                            // Add
                            dbOption = new Deathstar.Data.Models.PC_Option()
                            {
                                Name            = option.OptionName,
                                Order           = option.Order,
                                Type            = option.Type,
                                IsActive        = true,
                                CreatedBy       = context.Security.GetEmail(),
                                CreatedDateTime = DateTimeOffset.Now.ToString("yyyy/MM/dd HH:mm:ss.fff zzz")
                            };

                            // Set Primary Key
                            dbOption.SetPrimaryKey(productId.ToString(), option.OptionName);
                        }
                        else
                        {
                            // Update
                            dbOption.Order           = option.Order;
                            dbOption.Type            = option.Type;
                            dbOption.IsActive        = true;
                            dbOption.UpdatedBy       = context.Security.GetEmail();
                            dbOption.UpdatedDateTime = DateTimeOffset.Now.ToString("yyyy/MM/dd HH:mm:ss.fff zzz");
                        }

                        // Option Values
                        dbOption.Values = await _optionValueModel.Save(productId, dbOption, option.Values, trackingGuid);

                        // Custom Fields
                        if (option.CustomFields != null && option.CustomFields.Count > 0)
                        {
                            dbOption.CustomFields = await _customFieldModel.SaveGenericCustomField(option.CustomFields, "PC_ProductOption", dbOption.PC_Option_Id, trackingGuid);
                        }

                        // ExternalIds
                        if (option.ExternalIds != null && option.ExternalIds.Count > 0)
                        {
                            await _externalIdModel.SaveOrUpdateGenericExternalId(option.ExternalIds, "PC_ProductOption", dbOption.PC_Option_Id, trackingGuid);
                        }

                        dbOptions.Add(dbOption);
                    }
                }

                return(dbOptions);
            }
            catch (HttpResponseException webEx)
            {
                throw;
            }
            catch (Exception ex)
            {
                IG2000.Data.Utilities.Logging.LogTrackingEvent($"See logs for additional details", "Failed", LT319.Common.Utilities.Constants.TrackingStatus.Error, context, trackingGuid);
                IG2000.Data.Utilities.ErrorLogger.Report(ex.Message, "OptionModel.Save()", context, trackingGuid, System.Diagnostics.EventLogEntryType.Error);
                throw new HttpResponseException()
                      {
                          StatusCode = Microsoft.AspNetCore.Http.StatusCodes.Status500InternalServerError, ReasonPhrase = ex.Message
                      };
            }
        }