Exemplo n.º 1
0
        public static VMCampTopicShow  GetTopic(Guid id)
        {
            using (var db = new GarfielderEntities())
            {
                var dbm = db.Topics.Single(x => x.Id == id);
                if (dbm == null)
                {
                    return(null);
                }
                var vm = new VMCampTopicShow();
                vm.Id          = dbm.Id;
                vm.Title       = dbm.Title;
                vm.Slug        = dbm.Slug;
                vm.Url         = dbm.Url;
                vm.Grade       = dbm.Grade;
                vm.Description = dbm.Description;
                vm.ContentX    = dbm.ContentX;
                vm.Logo        = dbm.Logo;

                vm.Groups = dbm.Groups.ToList();

                vm.Tags = dbm.Tags.ToList();

                //vm.Attachments = dbm.XFiles.ToList();

                return(vm);
            };
        }
Exemplo n.º 2
0
        /// <summary>
        /// list all items by specified conditions
        /// </summary>
        /// <param name="published"></param>
        /// <param name="term"></param>
        /// <param name="callback"></param>
        /// <returns></returns>
        public static List <Topic> ListAll(bool published = false, string term = "", Action <List <Topic> > callback = null)
        {
            var retVal = new List <Topic>();

            //get topic data
            using (var db = new GarfielderEntities())
            {
                //TODO:searching optimize
                var q = default(IQueryable <Topic>);
                //filter-whether is published
                q = published ? db.Topics.Where(x => x.Published) : db.Topics;
                //filter-searching term
                if (!string.IsNullOrWhiteSpace(term))
                {
                    q = from obj in q
                        where obj.Title.ToLower().Contains(term)
                        select obj;
                }
                //sort
                retVal = q.OrderByDescending(x => x.CreatedAt).ToList();
                if (null != callback)
                {
                    callback(retVal);
                }
            }//using

            return(retVal);
        }
Exemplo n.º 3
0
        /// <summary>
        /// attach a file to a topic
        /// </summary>
        /// <param name="fileID"></param>
        /// <param name="topicID"></param>
        /// <returns></returns>
        public static Msg AttachToTopic(string fileID, string topicID)
        {
            var msg = new Msg();

            try
            {
                var dbm = default(XFile);
                var id  = Guid.Parse(fileID);
                using (var db = new GarfielderEntities())
                {
                    dbm = db.XFiles.SingleOrDefault(x => x.Id.Equals(id));
                    if (dbm == null)
                    {
                        msg.Error = true;
                        msg.Body  = string.Format("File with ID {0} has been deleted!", fileID);
                    }
                    else
                    {
                        AttachToTopic(topicID, db, dbm);
                    }
                }//using
            }
            catch (Exception ex)
            {
                msg.Error = true;
                msg.Body  = ex.Message;
                //TODO:log
            }
            return(msg);
        }
Exemplo n.º 4
0
        /// <summary>
        /// save a item.update if exists
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static dynamic Save(VMGroupEdit obj)
        {
            var msg = new Msg();
            var dbm = default(Group);

            obj.Slug = string.IsNullOrEmpty(obj.Slug) ? obj.Name.CHSToPinyin("-").ToLower() : obj.Slug.ToLower();

            //validate name existence
            if (!ValidateName(obj.Name, obj.Id.ToString()))
            {
                msg.Error = true;
                msg.Body  = string.Format("Name [{0}] exists,please choose another one!", obj.Name);
                return(msg);
            }
            ;

            using (var db = new GarfielderEntities())
            {
                if (obj.IsNew)//add new
                {
                    obj.Id        = Guid.NewGuid();
                    dbm           = new Group();
                    dbm.Id        = obj.Id;
                    dbm.CreatedAt = DateTime.Now;
                    //TODO
                    dbm.CreatedBy = "Sys";
                    db.AddToGroups(dbm);
                }
                else
                { //update
                    dbm = db.Groups.SingleOrDefault(x => x.Id.Equals(obj.Id));
                    if (dbm == null)
                    { //has been deleted!
                        msg.Error = true;
                        msg.Body  = string.Format("Obj {0} has been deleted!", obj.Id);
                        return(msg);
                    }
                }//if

                dbm.Name        = obj.Name;
                dbm.Slug        = Group.CheckSlug(db, obj.Slug);
                dbm.Description = obj.Description;

                dbm.ParentID = obj.ParentID.Equals(Guid.Empty) ? default(Guid?) : obj.ParentID;
                dbm.Level    = obj.Level;


                db.CommandTimeout = 0;
                db.SaveChanges();

                //clear cache
                ClearCache();

                if (dbm.Parent != null)
                {
                    obj.ParentName = dbm.Parent.Name;
                }
            };//using
            return(obj);
        }
Exemplo n.º 5
0
        /// <summary>
        /// delete by specified id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Msg DeleteByID(params Guid[] id)
        {
            var r = new Msg();

            if (id == null || id.Length == 0)
            {
                r.Error = true;
                r.Body  = "No selected items to be deleted!";
                return(r);
            }
            using (var db = new GarfielderEntities())
            {
                try
                {
                    var items = db.XFiles.Where(x => id.Contains(x.Id)).ToList();
                    items.ForEach(obj =>
                    {
                        obj.Topics.Clear();
                        db.XFiles.DeleteObject(obj);
                    });
                    db.SaveChanges();
                    ClearCache();
                }
                catch (Exception ex)
                {
                    r.Error = true;
                    r.Body  = ex.Message;
                }
            }            //using
            return(r);
        }
Exemplo n.º 6
0
        /// <summary>
        /// delete a record
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Msg Delete(Guid id)
        {
            var msg = new Msg();

            try
            {
                var goOn = TopicElected.Exists(id);
                if (!goOn)
                {
                    msg.Error = true;
                    msg.Body  = string.Format("Topic {0} doesn't exist!", id);
                }
                else
                {
                    var dbm = ListAll().Single(x => x.Id.Equals(id));
                    using (var db = new GarfielderEntities())
                    {
                        db.Attach(dbm);
                        db.DeleteObject(dbm);
                        db.SaveChanges();
                        ClearCache();
                    }//using
                }
            }
            catch (Exception ex)
            {
                msg.Error = true;
                msg.Body  = ex.Message;
            }
            return(msg);
        }
Exemplo n.º 7
0
        /// <summary>
        /// create a record
        /// </summary>
        /// <param name="id"></param>
        /// <param name="begin"></param>
        /// <param name="end"></param>
        /// <param name="who"></param>
        /// <returns></returns>
        public static Msg Create(Guid id, DateTime begin, DateTime end, string who)
        {
            var msg = new Msg();

            try
            {
                using (var db = new GarfielderEntities())
                {
                    var dbm = new TopicElected()
                    {
                        CreatedAt = DateTime.Now,
                        CreatedBy = who,
                        DateBegin = begin,
                        DateEnd   = end,
                        Id        = id
                    };
                    db.AddToTopicElecteds(dbm);
                    db.SaveChanges();
                    ClearCache();
                }
            }
            catch (Exception ex)
            {
                msg.Error = true;
                msg.Body  = ex.Message;
            }
            return(msg);
        }
Exemplo n.º 8
0
        }//autoslug

        /// <summary>
        /// list topic files for specified topic
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static VMXFileList ListFileData(Guid id)
        {
            var r = new VMXFileList();

            r.FileList = new List <VMXFileEdit>();
            if (id == Guid.Empty)
            {
                return(r);
            }
            using (var db = new GarfielderEntities())
            {
                var obj = db.Topics.SingleOrDefault(x => x.Id.Equals(id));
                if (obj == null)
                {
                    return(r);
                }
                obj.XFiles.ToList().ForEach(x => r.FileList.Add(new VMXFileEdit
                {
                    Id          = x.Id,
                    Title       = x.Title,
                    Extension   = x.Extension,
                    Description = x.Description,
                    UserName    = x.User.Name,
                    Name        = x.Name,
                    CreatedAt   = x.CreatedAt
                }));
                r.RefTopic = obj;
            } //using
            return(r);
        }     //ListFileData
Exemplo n.º 9
0
        /// <summary>
        /// update logo field
        /// </summary>
        /// <param name="img"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public static Msg UpdateLogo(string img, string id)
        {
            var msg = new Msg();

            if (string.IsNullOrWhiteSpace(img))
            {
                msg.Error = true;
                msg.Body  = "No Logo specified!";
                return(msg);
            }
            try
            {
                var gid   = Guid.Parse(id);
                var topic = default(Topic);
                using (var db = new GarfielderEntities())
                {
                    topic = db.Topics.SingleOrDefault(x => x.Id.Equals(gid));
                    if (topic != null)
                    {
                        topic.Logo = img;
                        db.SaveChanges();
                    }
                }
            }catch (Exception ex)
            {
                msg.Error = true;
                msg.Body  = ex.Message;
            }
            return(msg);
        }
Exemplo n.º 10
0
 private void fakeLogin()
 {
     using (var db = new GarfielderEntities()) {
         var user = db.Users.First();
         HttpContext.Items["CurrentUser"] = user;
         FormsAuthentication.SetAuthCookie(user.Name, false);
     }
 }
Exemplo n.º 11
0
        /// <summary>
        /// check the specified slug whether exists.if it exists we provide a new one
        /// </summary>
        /// <param name="db"></param>
        /// <param name="slug"></param>
        /// <returns></returns>
        public static string CheckSlug(GarfielderEntities db, string slug)
        {
            var tag = db.Groups.SingleOrDefault(x => x.Slug.Equals(slug, StringComparison.OrdinalIgnoreCase));

            if (null == tag)
            {
                return(slug);
            }
            return(string.Format("{0}{1}", slug, Utils.RandomStr(5)));
        }
Exemplo n.º 12
0
        /// <summary>
        /// get by id
        /// </summary>
        /// <param name="id"></param>
        /// <param name="dbToAttach"></param>
        /// <returns></returns>
        public static Group Get(Guid id, GarfielderEntities dbToAttach = null)
        {
            var obj = ListAll().SingleOrDefault(x => x.Id == id);

            if (dbToAttach != null && null != obj)
            {
                dbToAttach.Groups.Attach(obj);
            }

            return(obj);
        }
Exemplo n.º 13
0
        /// <summary>
        /// list all starred topics
        /// </summary>
        /// <returns></returns>
        public static List <Topic> ListAllStarred(Action <List <Topic> > callback = null)
        {
            var retVal = new List <Topic>();

            try
            {
                //get topic data
                using (var db = new GarfielderEntities())
                {
                    //TODO:searching optimize
                    var q = default(IQueryable <Topic>);
                    //filter-starred
                    q = from obj in db.Topics
                        where obj.Published && obj.TopicElected != null
                        select obj;
                    //sort
                    retVal = q.OrderByDescending(x => x.CreatedAt).ToList();
                    //parse icon
                    retVal.ForEach(x =>
                    {
                        var tempFile = db.XFiles.SingleOrDefault(y => y.Meta.IndexOf(x.Logo) >= 0) ??
                                       x.XFiles.OrderByDescending(y => y.Title).FirstOrDefault();

                        if (null != tempFile)
                        {
                            x.Icon = new VMXFileEdit
                            {
                                Id          = tempFile.Id,
                                Title       = tempFile.Title,
                                Extension   = tempFile.Extension,
                                Description = tempFile.Description,
                                UserName    = tempFile.User.Name,
                                Name        = tempFile.Name,
                                CreatedAt   = tempFile.CreatedAt
                            };
                        }
                    });
                    //callback);
                    if (null != callback)
                    {
                        callback(retVal);
                    }
                }//using
            }
            catch (Exception ex)
            {
                //TODO:log
            }

            return(retVal);
        }
Exemplo n.º 14
0
        /// <summary>
        /// get full topic data by slug
        /// </summary>
        /// <param name="slug"></param>
        /// <returns></returns>
        public static VMTopicFull GetTopic(string slug)
        {
            slug = slug.Trim().ToLower();
            var obj = new VMTopicFull();

            obj.Files  = new List <VMXFileEdit>();
            obj.Groups = new List <Group>();
            obj.Tags   = new List <Tag>();
            try
            {
                using (var db = new GarfielderEntities())
                {
                    var dbm = db.Topics.Single(x => x.Slug == slug);
                    if (dbm == null)
                    {
                        return(obj);
                    }


                    obj.Id          = dbm.Id;
                    obj.Title       = dbm.Title;
                    obj.Slug        = dbm.Slug;
                    obj.Desc        = dbm.Description;
                    obj.XContent    = dbm.ContentX;
                    obj.DateCreated = dbm.CreatedAt;
                    obj.Logo        = dbm.Logo;

                    obj.Groups = dbm.Groups.ToList();

                    obj.Tags = dbm.Tags.ToList();

                    dbm.XFiles.ToList().ForEach(x => obj.Files.Add(new VMXFileEdit
                    {
                        Id          = x.Id,
                        Title       = x.Title,
                        Extension   = x.Extension,
                        Description = x.Description,
                        UserName    = x.User.Name,
                        Name        = x.Name,
                        CreatedAt   = x.CreatedAt
                    }));
                };//using
            }
            catch (Exception ex)
            {
                //TODO:log
            }//try
            return(obj);
        }
Exemplo n.º 15
0
 /// <summary>
 /// topic statistics
 /// </summary>
 /// <returns></returns>
 public static dynamic TopicStat()
 {
     using (var db = new GarfielderEntities())
     {
         var sdate = DateTime.Today;
         var edate = sdate.AddDays(1);
         return(new{
             CntTopic = db.Topics.Count(),
             CntTopicToday = db.Topics.Count(x => x.CreatedAt >= sdate || x.CreatedAt < edate),
             CntGroup = Group.ListAll().Count,
             CntComment = db.TopicComments.Count(),
             CntTag = Tag.ListAll().Count
         });
     }
 }//TopicStat
Exemplo n.º 16
0
        /// <summary>
        /// save a tag object.Update if exists
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static dynamic Save(VMTagEdit obj)
        {
            var dbm = default(Tag);

            obj.Slug = string.IsNullOrEmpty(obj.Slug) ? obj.Name.CHSToPinyin("-").ToLower() : obj.Slug.ToLower();
            var msg = new Msg();

            //validate name
            if (!ValidateName(obj.Name, obj.Id.ToString()))
            {
                msg.Error = true;
                msg.Body  = string.Format("Name [{0}] exists!Please choose another one!", obj.Name);
                return(msg);
            }
            ;
            using (var db = new GarfielderEntities())
            {
                if (obj.IsNew)//add new
                {
                    obj.Id        = Guid.NewGuid();
                    dbm           = new Tag();
                    dbm.Id        = obj.Id;
                    dbm.CreatedAt = DateTime.Now;
                    //TODO
                    dbm.CreatedBy = "Sys";
                    db.AddToTags(dbm);
                }
                else
                { //update
                    dbm = db.Tags.SingleOrDefault(x => x.Id.Equals(obj.Id));
                    if (dbm == null)
                    { //has been deleted!
                        msg.Error = true;
                        msg.Body  = string.Format("Obj {0} has been deleted!", obj.Id);
                        return(msg);
                    }
                }//if



                dbm.Name          = obj.Name;
                dbm.Slug          = Tag.CheckSlug(db, obj.Slug);
                db.CommandTimeout = 0;
                db.SaveChanges();
                ClearCache();
            };//using
            return(obj);
        }
Exemplo n.º 17
0
        public static List <TopicElected> ListAll()
        {
            using (var db = new GarfielderEntities())
            {
                if (_Items == null)
                {
                    lock (_SyncRoot)
                    {
                        if (_Items == null)
                        {
                            _Items = db.TopicElecteds.ToList();
                        }
                    }
                }

                return(_Items);
            }//using
        }
Exemplo n.º 18
0
        /// <summary>
        /// get attachment ids
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List <Guid> GetAttachments(Guid id)
        {
            var retVal = new List <Guid>();

            try
            {
                using (var db = new GarfielderEntities())
                {
                    var obj = db.Topics.Single(x => x.Id.Equals(id));
                    obj.XFiles.ToList().ForEach(y => retVal.Add(y.Id));
                }
            }
            catch (Exception ex)
            {
                //do nothing
            }
            return(retVal);
        }
Exemplo n.º 19
0
        /// <summary>
        /// list all items
        /// </summary>
        /// <returns></returns>
        public static List <XFile> ListAll()
        {
            using (var db = new GarfielderEntities())
            {
                if (_Items == null)
                {
                    lock (_SyncRoot)
                    {
                        if (_Items == null)
                        {
                            _Items = db.XFiles.ToList();
                            _Items.ForEach(x => x.UserName = x.User.Name);
                        }
                    }
                }

                return(_Items);
            }//using
        }
        public ActionResult ListUser()
        {
            var vm = CreateViewData <VMUserList>();

            using (var db = new GarfielderEntities()) {
                var users = db.Users.ToList();
                vm.UserList = new List <VMUserEdit>();
                users.ForEach(x => {
                    vm.UserList.Add(new VMUserEdit {
                        Name     = x.Name,
                        Nickname = x.Nickname,
                        Email    = x.Email,
                        Password = x.Password,
                        Id       = x.Id
                    });
                });
            }
            return(View(vm));
        }
Exemplo n.º 21
0
        /// <summary>
        /// list all group data.TODO:Don't use Group!You should define an interface type such as IGroupData
        /// </summary>
        /// <returns></returns>
        public static List <Group> ListAll()
        {
            using (var db = new GarfielderEntities())
            {
                if (_Groups == null)
                {
                    lock (_SyncRoot)
                    {
                        if (_Groups == null)
                        {
                            _Groups = db.Groups.ToList();
                            _Groups.ForEach(x => x.CntTopic = x.Topics.Count);
                        }
                    }
                }

                return(_Groups);
            }            //using
        }
Exemplo n.º 22
0
        /// <summary>
        /// add a simple tag
        /// </summary>
        /// <param name="name"></param>
        /// <param name="slug"></param>
        /// <returns></returns>
        public static VMTagEdit QuickAddTag(string name, string slug = null, string user = "******")
        {
            var retVal = new VMTagEdit();

            if (string.IsNullOrWhiteSpace(name))
            {
                retVal.Error = true;
                retVal.Msg   = "Tag name can't be blank";
                return(retVal);
            }
            ;

            retVal.Name = name;
            retVal.Slug = string.IsNullOrWhiteSpace(slug) ? name.CHSToPinyin("-") : slug.CHSToPinyin("-");
            retVal.Slug = retVal.Slug.ToLower();

            using (var db = new GarfielderEntities()) {
                var dbm = db.Tags.SingleOrDefault(x => x.Name.Equals(name, StringComparison.OrdinalIgnoreCase));
                if (dbm != null)
                {
                    retVal.Id = dbm.Id;
                }
                else
                {
                    retVal.Id = Guid.NewGuid();
                    //add a new tag
                    dbm           = new Tag();
                    dbm.Id        = retVal.Id;
                    dbm.Name      = retVal.Name;
                    dbm.Slug      = CheckSlug(db, retVal.Slug);
                    dbm.CreatedAt = DateTime.Now;
                    //TODO:
                    dbm.CreatedBy = user;
                    db.AddToTags(dbm);
                    //persist
                    db.CommandTimeout = 0;
                    db.SaveChanges();
                    ClearCache();
                };
            };//using
            return(retVal);
        }
Exemplo n.º 23
0
        }//SaveFile

        private static void AttachToTopic(string topicID, GarfielderEntities db, XFile file)
        {
            //refID proccess
            var objID = Guid.Parse(topicID);

            if (objID == Guid.Empty)
            {
                return;
            }
            var obj = file.Topics.SingleOrDefault(x => x.Id == objID);

            //exists
            if (obj != null)
            {
                return;
            }
            //attach
            file.Topics.Add(db.Topics.Single(x => x.Id.Equals(objID)));
            db.SaveChanges();
        }
        public ActionResult EditUser(Guid?id)
        {
            if (id == null || id == Guid.Empty)
            {
                return(NewUser());
            }
            ;
            var vm = CreateViewData <VMUserEdit>();

            using (var db = new GarfielderEntities())
            {
                var dbModel = db.Users.Single(x => x.Id == id.Value);
                vm.Id       = dbModel.Id;
                vm.Name     = dbModel.Name;
                vm.Nickname = dbModel.Nickname;
                vm.Email    = dbModel.Email;
                vm.Password = dbModel.Password;
            };
            return(View(vm));
        }
 public ActionResult EditFile(VMXFileEdit obj)
 {
     if (this.ModelState.IsValid)
     {
         using (var db = new GarfielderEntities())
         {
             var dbm = db.XFiles.SingleOrDefault(x => x.Id == obj.Id);
             if (dbm != null)
             {
                 dbm.Title         = obj.Title;
                 dbm.Description   = obj.Description;
                 db.CommandTimeout = 0;
                 db.SaveChanges();
             }
             ;
         };
     }
     ;
     return(View(obj));
 }
Exemplo n.º 26
0
        }//TopicStat

        /// <summary>
        /// check the specified slug whether exists.if it exists we provide a new one
        /// </summary>
        /// <param name="slug"></param>
        /// <param name="db"></param>
        /// <returns></returns>
        public static string AutoSlug(string slug, GarfielderEntities db = null)
        {
            if (string.IsNullOrWhiteSpace(slug))
            {
                return(Utils.RandomStr(8));
            }
            slug = slug.Trim().CHSToPinyin("-").ToLower().RemoveWhitespace();
            //slug length limit
            slug = slug.Length > 45 ? slug.Substring(0, 45) : slug;
            var needDispose = false;

            try
            {
                if (db == null)
                {
                    db          = new GarfielderEntities();
                    needDispose = true;
                }


                var obj = db.Topics.SingleOrDefault(x => x.Slug.Equals(slug, StringComparison.OrdinalIgnoreCase));
                if (null != obj)
                {
                    slug = string.Format("{0}{1}", slug, Utils.RandomStr(5));
                }
            }
            catch (Exception ex)
            {
                //TODO:log the exception
                slug = string.Format("{0}{1}", slug, Utils.RandomStr(5));
            }finally
            {
                if (needDispose)
                {
                    db.Dispose();
                }
            }//try

            return(slug);
        }//autoslug
Exemplo n.º 27
0
        /// <summary>
        /// validate a slug
        /// </summary>
        /// <param name="slug">slug</param>
        /// <param name="db">GarfielderEntities instance</param>
        /// <returns></returns>
        public static Msg ValidateSlug(string slug, GarfielderEntities db = null)
        {
            var r = new Msg();

            if (string.IsNullOrWhiteSpace(slug))
            {
                r.Error           = true;
                r.Body            = "Slug can't be empty!";
                r.Context["Slug"] = Utils.RandomStr(8);
                return(r);
            }
            var isNewConnection = db == null;

            db = db ?? new GarfielderEntities();
            r.Context["Slug"] = slug;
            try
            {
                var topic = db.Topics.SingleOrDefault(x => x.Slug.Equals(slug, StringComparison.OrdinalIgnoreCase));
                if (topic == null)
                {
                    return(r);
                }
                r.Error           = true;
                r.Body            = string.Format("Slug [{0}] has been used by another topic!", slug);
                r.Context["Slug"] = string.Format("{0}{1}", slug, Utils.RandomStr(4));
            }
            catch (Exception ex)
            {
                r.Error = true;
                r.Body  = string.Format("Error:{0}", ex.Message);
            }finally
            {
                if (isNewConnection)
                {
                    db.Dispose();
                }
            }
            return(r);
        }
Exemplo n.º 28
0
        }     //ListFileData

        /// <summary>
        /// Detach files
        /// </summary>
        /// <param name="topicID"></param>
        /// <param name="fileID"></param>
        /// <returns></returns>
        public static Msg DetachFiles(Guid topicID, params Guid[] fileID)
        {
            var msg = new Msg();

            try
            {
                using (var db = new GarfielderEntities())
                {
                    var obj = db.Topics.SingleOrDefault(x => x.Id.Equals(topicID));
                    if (obj == null || obj.XFiles.Count == 0)
                    {
                        msg.Error = true;
                        msg.Body  = string.Format("Topic with id [{0}] not exists or it has no attachments!", topicID);
                        return(msg);
                    }//if

                    //delete all attached files
                    if (fileID == null || fileID.Length == 0)
                    {
                        obj.XFiles.Clear();
                        db.SaveChanges();
                        return(msg);
                    }//if

                    //delete specified files
                    var items = obj.XFiles.Where(x => !fileID.Contains(x.Id));
                    obj.XFiles.Clear();
                    items.ToList().ForEach(x => obj.XFiles.Add(x));
                    db.SaveChanges();
                } //using
            }catch (Exception ex)
            {
                //TODO:log
                msg.Error = true;
                msg.Body  = ex.Message;
            }//try
            return(msg);
        }
 public ActionResult EditUser(VMUserEdit obj)
 {
     if (this.ModelState.IsValid)
     {
         var dbm = new User();
         dbm.Id        = Guid.NewGuid();
         dbm.Name      = obj.Name;
         dbm.Nickname  = obj.Nickname;
         dbm.Email     = obj.Email;
         dbm.Password  = obj.Password;
         dbm.CreatedAt = DateTime.Now;
         //TODO:
         dbm.CreatedBy = "Sys";
         using (var db = new GarfielderEntities())
         {
             db.CommandTimeout = 0;
             db.AddToUsers(dbm);
             db.SaveChanges();
         };
     }
     ;
     return(View(obj));
 }
Exemplo n.º 30
0
        /// <summary>
        /// Save current file uploaded
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public static VMXFileEdit SaveFile(HttpRequestBase req)
        {
            var vm = new VMXFileEdit();

            vm.NoFlash = true;


            if (req.Files.Count == 0 || req.Files[0].FileName == "" || req.Files[0].ContentLength == 0)
            {
                vm.Error = true;
                vm.Msg   = "No file selected!Please select a file to upload!";
                return(vm);
            }
            ;
            var refid    = req.Params["RefId"];
            var uid      = req.Params["UserID"];
            var uname    = req.Params["UserName"];
            var fileName = req.Files[0].FileName.CHSToPinyin("-").ToLower();
            var path0    = getFileName(uname, fileName);                                           //虚拟路径
            var path1    = Path.Combine(_uploadsFolder, uname, DateTime.Now.ToString("yyyyMMdd")); //物理路径

            //db op
            using (var db = new GarfielderEntities())
            {
                var dbm = db.XFiles.SingleOrDefault(x => x.Name == path0);
                if (dbm == null)
                {
                    //save to disk
                    vm.Name = path0;

                    if (!Directory.Exists(path1))
                    {
                        Directory.CreateDirectory(path1);
                    }

                    try
                    {
                        vm.Title       = req.Files[0].FileName;
                        vm.Description = vm.Title;
                        vm.Extension   = vm.Name.Substring(vm.Name.LastIndexOf("."));

                        vm.Id        = Guid.NewGuid();
                        vm.CreatedAt = DateTime.Now;

                        path1 = string.Format("{0}\\{1}", path1, fileName);
                        req.Files[0].SaveAs(path1);

                        //图片缩略图处理);
                        if (IsImg(vm.Extension))
                        {
                            //大图800x600
                            var tempObj = ImageResizer.GetThumbnail(path1, GetImgName(path1, ImageFlags.L800X600), 800, 600, true);
                            vm.MetaData.Width  = tempObj.RawSize.Width;
                            vm.MetaData.Height = tempObj.RawSize.Height;

                            //原图
                            vm.MetaData.AddThumb(ImageFlags.RAW,
                                                 string.Format("{0}{1}",
                                                               Garfielder.Web.Utils.AbsoluteWebRoot, vm.Name),
                                                 vm.MetaData.Width, vm.MetaData.Height);

                            if (!tempObj.Error)
                            {
                                vm.MetaData.AddThumb(ImageFlags.L800X600,
                                                     string.Format("{0}{1}_800x600{2}",
                                                                   Garfielder.Web.Utils.AbsoluteWebRoot, vm.Name1,
                                                                   vm.Extension), tempObj.NewSize.Width, tempObj.NewSize.Height);
                            }
                            //中大图500x500
                            tempObj = ImageResizer.GetThumbnail(path1, GetImgName(path1, ImageFlags.L500X500), 500, 500, true);
                            if (!tempObj.Error)
                            {
                                vm.MetaData.AddThumb(ImageFlags.L500X500,
                                                     string.Format("{0}{1}_500x500{2}",
                                                                   Garfielder.Web.Utils.AbsoluteWebRoot, vm.Name1,
                                                                   vm.Extension), tempObj.NewSize.Width, tempObj.NewSize.Height);
                            }
                            //中
                            tempObj = ImageResizer.GetThumbnail(path1, GetImgName(path1, ImageFlags.M300X300), 300, 300, true);
                            if (!tempObj.Error)
                            {
                                vm.MetaData.AddThumb(ImageFlags.M300X300,
                                                     string.Format("{0}{1}_300x300{2}",
                                                                   Garfielder.Web.Utils.AbsoluteWebRoot, vm.Name1,
                                                                   vm.Extension), tempObj.NewSize.Width, tempObj.NewSize.Height);
                            }
                            //中小图
                            tempObj = ImageResizer.GetThumbnail(path1, GetImgName(path1, ImageFlags.M160X160), 160, 160, true);
                            if (!tempObj.Error)
                            {
                                vm.MetaData.AddThumb(ImageFlags.M160X160,
                                                     string.Format("{0}{1}_160x160{2}",
                                                                   Garfielder.Web.Utils.AbsoluteWebRoot, vm.Name1,
                                                                   vm.Extension), tempObj.NewSize.Width, tempObj.NewSize.Height);
                            }
                            tempObj = ImageResizer.GetThumbnail(path1, GetImgName(path1, ImageFlags.S160X100), 160, 100);
                            if (!tempObj.Error)
                            {
                                vm.MetaData.AddThumb(ImageFlags.S160X100,
                                                     string.Format("{0}{1}_160x100{2}",
                                                                   Garfielder.Web.Utils.AbsoluteWebRoot, vm.Name1,
                                                                   vm.Extension), tempObj.NewSize.Width, tempObj.NewSize.Height);
                            }
                            //小
                            tempObj = ImageResizer.GetThumbnail(path1, GetImgName(path1, ImageFlags.S64X64), 64, 64);
                            if (!tempObj.Error)
                            {
                                vm.MetaData.AddThumb(ImageFlags.S64X64,
                                                     string.Format("{0}{1}_64x64{2}",
                                                                   Garfielder.Web.Utils.AbsoluteWebRoot, vm.Name1,
                                                                   vm.Extension), tempObj.NewSize.Width, tempObj.NewSize.Height);
                            }
                        }
                        ; //IsImg

                        dbm             = new XFile();
                        dbm.Id          = vm.Id;
                        dbm.Name        = path0;
                        dbm.Url         = path0;
                        dbm.Title       = vm.Title;
                        dbm.Description = vm.Description;
                        dbm.Extension   = vm.Extension;
                        dbm.CreatedAt   = dbm.ModifiedAt = vm.CreatedAt;
                        dbm.Meta        = vm.Meta;

                        dbm.UserID        = Guid.Parse(uid);
                        db.CommandTimeout = 0;
                        db.AddToXFiles(dbm);
                        db.SaveChanges();
                        ClearCache();
                        //attach to topic
                        AttachToTopic(refid, db, dbm);
                    }
                    catch (Exception ex)
                    {
                        //TODO:log
                        vm.Error = true;
                        vm.Msg   = ex.Message;
                    }//TRY
                }
                else
                {
                    //FILE Exists
                    vm.Error = true;
                    vm.Msg   = string.Format("File {0} exists!", path0);
                    //attach to topic
                    AttachToTopic(refid, db, dbm);
                };
            };//using
            return(vm);
        }//SaveFile