Esempio n. 1
0
        public static bool UpdateCover(string id)
        {
            if (!General.IsNullable(id))
            {
                using (WMContext context = new WMContext())
                {
                    GoodImages model = context.GoodImages.Find(id);

                    if (model != null)
                    {
                        GoodImages old = context.GoodImages.Where(gi => gi.GoodId.Equals(model.GoodId) && gi.IsCover).FirstOrDefault();
                        if (old != null)
                        {
                            old.IsCover = false;
                        }
                        model.IsCover = true;

                        context.SaveChanges();
                        return(true);
                    }
                }
            }

            return(false);
        }
Esempio n. 2
0
        public bool Add()
        {
            if (this.Valid())
            {
                this.Id      = General.UniqueString(this.Id);
                this.AddDate = DateTime.Now;

                using (WMContext context = new WMContext())
                {
                    this.IsCover = (context.GoodImages.Where(gi => gi.GoodId.Equals(this.GoodId)).Count() == 0);

                    GoodImages model = new GoodImages {
                        Id      = this.Id,
                        GoodId  = this.GoodId,
                        URL     = this.URL,
                        IsCover = this.IsCover,
                        AddDate = this.AddDate
                    };

                    context.GoodImages.Add(model);
                    context.SaveChanges();
                }

                return(true);
            }

            return(false);
        }
Esempio n. 3
0
        public static bool Delete(string id)
        {
            if (!General.IsNullable(id))
            {
                string fileName = null;

                using (WMContext context = new WMContext())
                {
                    GoodImages model = context.GoodImages.Find(id);

                    if (model != null)
                    {
                        fileName = "~" + model.URL;
                        GoodImages newCover = null;

                        if (model.IsCover)
                        {
                            newCover = (
                                from gi in context.GoodImages
                                where gi.GoodId.Equals(model.GoodId) &&
                                gi.Id != model.Id
                                orderby gi.AddDate descending
                                select gi
                                ).FirstOrDefault();

                            if (newCover != null)
                            {
                                newCover.IsCover = true;
                            }
                        }

                        context.GoodImages.Remove(model);
                        context.SaveChanges();
                    }
                }

                return(FileHelper.DeleteFile(fileName));
            }

            return(false);
        }
Esempio n. 4
0
        public bool Add()
        {
            if (this.Valid())
            {
                this.Id = General.UniqueString(this.Id);
                this.AddDate = DateTime.Now;

                using (WMContext context = new WMContext())
                {
                    this.IsCover = (context.GoodImages.Where(gi => gi.GoodId.Equals(this.GoodId)).Count() == 0);

                    GoodImages model = new GoodImages {
                        Id = this.Id,
                        GoodId = this.GoodId,
                        URL = this.URL,
                        IsCover = this.IsCover,
                        AddDate = this.AddDate
                    };

                    context.GoodImages.Add(model);
                    context.SaveChanges();
                }

                return true;
            }

            return false;
        }