)>}]
شركة التطبيقات المتكاملة لتصميم وبرمجة البرمجيات الخاصة ش.ش.و.
Integrated Applications Programming Company
Home » Code Library » Product (Ia.Statistics.Cl.Models)

Public general use code classes and xml files that we've compiled and used over the years:

    1: using System;
    2: using System.ComponentModel.DataAnnotations.Schema;
    3: using System.ComponentModel.DataAnnotations;
    4: using System.Collections.Generic;
    5: using System.Linq;
    6: using Microsoft.EntityFrameworkCore;
    7:  
    8: namespace Ia.Statistics.Cl.Models
    9: {
   10:     ////////////////////////////////////////////////////////////////////////////
   11:  
   12:     /// <summary publish="true">
   13:     ///
   14:     /// </summary>
   15:     /// 
   16:     /// <remarks> 
   17:     /// Copyright © 2006-2025 Jasem Y. Al-Shamlan (info@ia.com.kw), Integrated Applications - Kuwait. All Rights Reserved.
   18:     ///
   19:     /// This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
   20:     /// the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
   21:     ///
   22:     /// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
   23:     /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   24:     /// 
   25:     /// You should have received a copy of the GNU General Public License along with this library. If not, see http://www.gnu.org/licenses.
   26:     /// 
   27:     /// Copyright notice: This notice may not be removed or altered from any source distribution.
   28:     /// </remarks> 
   29:     public class Product
   30:     {
   31:         private static List<Ia.Statistics.Cl.Models.Product> productList;
   32:         private static Dictionary<int, List<Ia.Statistics.Cl.Models.Product>> siteIdToProductListDictionary;
   33:  
   34:         private static readonly object objectLock = new object();
   35:  
   36:         /// <summary/>
   37:         [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
   38:         public string Id { get; set; }
   39:  
   40:         /// <summary/>
   41:         public int SiteId { get; set; }
   42:  
   43:         /// <summary/>
   44:         public string Sku { get; set; }
   45:  
   46:         /// <summary/>
   47:         public string Name { get; set; }
   48:  
   49:         /// <summary/>
   50:         public string Url { get; set; }
   51:  
   52:         /// <summary/>
   53:         //public int QuantityAllowedToBuy { get; set; }
   54:  
   55:         /// <summary/>
   56:         public DateTime Created { get; set; }
   57:  
   58:         /// <summary/>
   59:         public DateTime Updated { get; set; }
   60:  
   61:         /// <summary/>
   62:         [ForeignKey("Category_Id")]
   63:         public virtual Category Category { get; set; }
   64:  
   65:         /// <summary/>
   66:         public virtual ICollection<ProductPriceSpot> ProductPriceSpots { get; set; }
   67:  
   68:         /// <summary/>
   69:         public virtual ICollection<ProductStockSpot> ProductStockSpots { get; set; }
   70:  
   71:         ////////////////////////////////////////////////////////////////////////////
   72:  
   73:         /// <summary>
   74:         ///
   75:         /// </summary>
   76:         public static string ProductId(Site site, string sku)
   77:         {
   78:             string id;
   79:  
   80:             if (site != null && !string.IsNullOrEmpty(sku))
   81:             {
   82:                 var siteShortName = Ia.Statistics.Cl.Models.Site.SiteShortNameFromId(site.Id);
   83:  
   84:                 id = siteShortName + ":" + sku;
   85:             }
   86:             else id = string.Empty;
   87:  
   88:             return id;
   89:         }
   90:  
   91:         ////////////////////////////////////////////////////////////////////////////
   92:  
   93:         /// <summary>
   94:         ///
   95:         /// </summary>
   96:         public static string SiteShortNameFromProductId(string productId)
   97:         {
   98:             string siteShortName;
   99:  
  100:             if (!string.IsNullOrEmpty(productId))
  101:             {
  102:                 var indexOfColon = productId.IndexOf(":");
  103:  
  104:                 siteShortName = productId.Substring(0, indexOfColon);
  105:             }
  106:             else siteShortName = string.Empty;
  107:  
  108:             return siteShortName;
  109:         }
  110:  
  111:         ////////////////////////////////////////////////////////////////////////////
  112:  
  113:         /// <summary>
  114:         ///
  115:         /// </summary>
  116:         public static Ia.Statistics.Cl.Models.Product ById(string productId)
  117:         {
  118:             var product = new Ia.Statistics.Cl.Models.Product();
  119:  
  120:             if (!string.IsNullOrEmpty(productId))
  121:             {
  122:                 using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  123:                 {
  124:                     product = (from p in db.Products
  125:                                where p.Id == productId
  126:                                select p).SingleOrDefault();
  127:                 }
  128:             }
  129:  
  130:             return product;
  131:         }
  132:  
  133:         ////////////////////////////////////////////////////////////////////////////
  134:  
  135:         /// <summary>
  136:         ///
  137:         /// </summary>
  138:         public static List<Ia.Statistics.Cl.Models.Product> List
  139:         {
  140:             get
  141:             {
  142:                 if (productList == null || productList.Count == 0)
  143:                 {
  144:                     lock (objectLock)
  145:                     {
  146:                         using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  147:                         {
  148:                             productList = (from p in db.Products
  149:                                            select p).AsNoTracking().ToList();
  150:                         }
  151:                     }
  152:                 }
  153:  
  154:                 return productList;
  155:             }
  156:         }
  157:  
  158:         ////////////////////////////////////////////////////////////////////////////
  159:  
  160:         /// <summary>
  161:         ///
  162:         /// </summary>
  163:         public static List<Ia.Statistics.Cl.Models.Product> ListBySiteId(int siteId)
  164:         {
  165:             if (siteIdToProductListDictionary == null || siteIdToProductListDictionary.Count == 0)
  166:             {
  167:                 siteIdToProductListDictionary = new Dictionary<int, List<Ia.Statistics.Cl.Models.Product>>();
  168:             }
  169:  
  170:             if (!siteIdToProductListDictionary.ContainsKey(siteId) || siteIdToProductListDictionary[siteId] == null)
  171:             {
  172:                 siteIdToProductListDictionary[siteId] = new List<Ia.Statistics.Cl.Models.Product>();
  173:  
  174:                 lock (objectLock)
  175:                 {
  176:                     using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  177:                     {
  178:                         siteIdToProductListDictionary[siteId] = (from p in db.Products
  179:                                                                  where p.SiteId == siteId
  180:                                                                  select p).ToList();
  181:                     }
  182:                 }
  183:             }
  184:  
  185:             return siteIdToProductListDictionary[siteId];
  186:         }
  187:  
  188:         ////////////////////////////////////////////////////////////////////////////
  189:  
  190:         /// <summary>
  191:         ///
  192:         /// </summary>
  193:         public static List<string> UrlListBySiteId(int siteId)
  194:         {
  195:             var urlList = new List<string>();
  196:  
  197:             using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  198:             {
  199:                 urlList = (from p in db.Products
  200:                            where p.SiteId == siteId
  201:                            select p.Url).ToList();
  202:             }
  203:  
  204:             return urlList;
  205:         }
  206:  
  207:         ////////////////////////////////////////////////////////////////////////////
  208:  
  209:         /// <summary>
  210:         ///
  211:         /// </summary>
  212:         public static void Create(Ia.Statistics.Cl.Models.Product newProduct)
  213:         {
  214:             using (var db = new Ia.Statistics.Cl.Models.StatisticsDbContext())
  215:             {
  216:                 var product = (from a in db.Products where a.Id == newProduct.Id select a).SingleOrDefault();
  217:  
  218:                 if (product == null)
  219:                 {
  220:                     newProduct.Created = newProduct.Updated = DateTime.UtcNow.AddHours(3);
  221:  
  222:                     db.Products.Add(newProduct);
  223:                     db.SaveChanges();
  224:                 }
  225:                 else
  226:                 {
  227:                 }
  228:             }
  229:         }
  230:  
  231:         ////////////////////////////////////////////////////////////////////////////
  232:         ////////////////////////////////////////////////////////////////////////////
  233:     }
  234: }