コード例 #1
0
ファイル: AlbumDAO.cs プロジェクト: sivarajankumar/famsam
 // current user create new album
 // return -1 if fail
 // return 0 if success
 public static int CreateAlbum(AlbumDTO albumNew)
 {
     using (var context = new CF_FamsamEntities())
     {
         if (albumNew == null) return -1;
         User user = context.User.FirstOrDefault(u => u.email.Equals(albumNew.AuthorEmail));
         if (user == null) return -1;
         GeneralPost post = new GeneralPost();
         post.Id = DateTime.Now.Millisecond;
         post.description = albumNew.Description;
         post.lastUpdate = DateTime.Now;
         post.createUserId = user.id;
         Album album = new Album();
         album.id = post.Id;
         album.title = albumNew.Title;
         try
         {
             foreach (var photo in albumNew.ListPhoto)
             {
                 album.Photo.Add(context.Photo.FirstOrDefault(p => p.id == photo.Id));
             }
             context.GeneralPost.Add(post);
             context.Album.Add(album);
             context.SaveChanges();
         } catch (Exception ex)
         {
             Debug.WriteLine("Exception: " + ex.StackTrace);
             return -1;
         }
         return 0;
     }
 }
コード例 #2
0
ファイル: Global.asax.cs プロジェクト: sivarajankumar/famsam
        protected void InitializeDB()
        {
            using (CF_FamsamEntities context = new CF_FamsamEntities())
            {
                //create user role
                UserRole userRole = context.UserRole.Find(UserRole.LOGGED_IN_ROLE);
                if ( userRole == null)
                {
                    userRole = new UserRole();
                    userRole.rolename = UserRole.LOGGED_IN_ROLE;
                    context.UserRole.Add(userRole);
                }
                
                //create user
                User createUser = (from u in context.User where u.email == "mrbean" select u).FirstOrDefault<User>();
                if (createUser == null)
                {
                    createUser = new User
                    {
                        id = DateTime.Now.Millisecond,
                        email = "*****@*****.**",
                        password = "******",
                        firstname = "Lup",
                        lastname = "Bean",
                        UserRole = userRole,
                        role = userRole.rolename
                    };
                    context.User.Add(createUser);
                }
                
                //create session
                string token = Base64Utils.Base64Encode("mrbean:mrbean");
                Session session = context.Session.Find(token);
                if (session == null)
                {
                    session = new Session
                    {
                        token = token,
                        expired = new DateTime(2100, 1, 1),
                        User = createUser,
                    };
                    context.Session.Add(session);
                }
                
                //new post for photo
                DateTime thisTime = DateTime.Now;
                GeneralPost post = new GeneralPost();
                post.Id = thisTime.Millisecond;
                post.lastUpdate = thisTime;
                post.description = "haha";
                post.CreateUser = createUser;
                post.createUserId = createUser.id;
                post.postType = GeneralPost.PHOTO_POST_TYPE;
                context.GeneralPost.Add(post);
                
                Photo photo = new Photo();
                photo.Post = post;
                photo.url = "http://photo.url/nothing.jpg";
                context.Photo.Add(photo);
                try 
                { 
                    context.SaveChanges(); 
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Exception on Initialize DB Sample: " + ex);
                }
            }

        }
コード例 #3
0
ファイル: PhotoDAO.cs プロジェクト: sivarajankumar/famsam
        /// <summary>
        /// Create a Photo entity of non-album.
        /// </summary>
        /// <param name="photoDTO"></param>
        /// <returns>-1 if user not found</returns>
        public static int AddPhoto(PhotoDTO photoDTO)
        {
            //get user
            User user;
            using (var db = new CF_FamsamEntities())
            {
                user = db.User.Find(photoDTO.AuthorId);
                if (user == null)
                {
                    return -1;
                }

                //create photo and post
                DateTime lastUpdate = DateTime.Now;
                Photo photo = new Photo();
                photo.id = lastUpdate.Millisecond;
                photo.url = photoDTO.ImageURL;
                photo.badQuality = photoDTO.BadQuality;
                GeneralPost post = new GeneralPost();
                post.Id = photo.id;
                post.postType = GeneralPost.PHOTO_POST_TYPE;
                post.Photo = photo;
                post.createUserId = user.id;
                post.CreateUser = user;
                post.lastUpdate = lastUpdate;

                foreach (string tagname in photoDTO.tags)
                {
                    Tag tag = db.Tag.Find(tagname);
                    if (tag == null)
                    {
                        tag = new Tag();
                        tag.name = tagname;
                        db.Tag.Add(tag);
                    }
                    post.Tag.Add(tag);
                }
                try { db.GeneralPost.Add(post); }
                catch (Exception ex)
                {
                    Debug.WriteLine("Exception on create photo: " + ex.ToString());
                }

                return 1;
            }

        }