Ejemplo n.º 1
0
        // list albums by story of a user
        public static List<AlbumDTO> ListAlbumsByStory(long userId, int page, int size, long storyId)
        {
            using (var context = new CF_FamsamEntities())
            {
                var generalPost = context.GeneralPost.FirstOrDefault(g => g.Id == storyId);
                var listAlbum = generalPost.Story.Album.Skip((page -1) * size).Take(size).ToList();
                AlbumDTO albumInstance = new AlbumDTO();
                List<AlbumDTO> listAlbumDTO = new List<AlbumDTO>();
                foreach (var item in listAlbum)
                {
                    albumInstance.Id = item.id;
                    albumInstance.Title = item.title;
                    albumInstance.Description = item.Post.description;
                    albumInstance.LastUpdate = item.Post.lastUpdate;
                    albumInstance.AuthorFirstname = item.Post.CreateUser.firstname;
                    albumInstance.AuthorLastname = item.Post.CreateUser.lastname;
                    albumInstance.AuthorEmail = item.Post.CreateUser.email;
                    albumInstance.ListPhoto = PhotoDAO.ListPhotosByAlbum(userId, page, size, item.id);

                    listAlbumDTO.Add(albumInstance);
                }

                return (listAlbumDTO.Count() > 0) ? listAlbumDTO : new List<AlbumDTO>(0);
            }
        }
Ejemplo n.º 2
0
        public static List<PhotoDTO> ListOtherUserPhotos(long currentUserId, long otherUserId,long storyId, long albumId, int page, int size)
        {
            List<PhotoDTO> photoDTOs = new List<PhotoDTO>();
            using (var db = new CF_FamsamEntities())
            {
                //query album

                Album album = db.Story.Find(storyId).Album.Where<Album>(a => a.id == albumId).First();
                if (album != null)
                {
                    foreach (var photo in album.Photo)
                    {
                        PhotoDTO photoDTO = new PhotoDTO();
                        photoDTO.Id = photo.id;
                        photoDTO.AuthorFirstName = photo.Post.CreateUser.firstname;
                        photoDTO.AuthorLastName = photo.Post.CreateUser.lastname;
                        photoDTO.LastUpdate = photo.Post.lastUpdate;
                        photoDTO.Description = photo.Post.description;
                        photoDTO.ImageURL = photo.url;
                        photoDTO.BadQuality = photo.badQuality;
                        photoDTOs.Add(photoDTO);
                    }
                }
            }
            return photoDTOs;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// List all photos
        /// </summary>
        /// <param name="userId">Id of user</param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns>List of photos (0 item at least)</returns>
        public static List<PhotoDTO> ListCurrentPhotos(long userId, int page, int size)
        {
            List<GeneralPost> photos = new List<GeneralPost>();
            List<PhotoDTO> result = new List<PhotoDTO>();
            using (var db = new CF_FamsamEntities())
            {
                var photoQuery = from p in db.GeneralPost
                                 where p.postType == GeneralPost.PHOTO_POST_TYPE
                                 orderby p.lastUpdate descending
                                 select p;
                    photos = photoQuery.Skip((page - 1) * size)
                                   .Take(size)
                                   .ToList<GeneralPost>();

                //to dto
                foreach (GeneralPost photo in photos)
                {
                    PhotoDTO photoDTO = new PhotoDTO();
                    photoDTO.Id = photo.Id;
                    photoDTO.AuthorFirstName = photo.CreateUser.firstname;
                    photoDTO.AuthorLastName = photo.CreateUser.lastname;
                    photoDTO.LastUpdate = photo.lastUpdate;
                    photoDTO.Description = photo.description;
                    photoDTO.ImageURL = photo.Photo.url;
                    photoDTO.BadQuality = photo.Photo.badQuality;
                    result.Add(photoDTO);
                }
            }
            return result;
        }
Ejemplo n.º 4
0
        // list all albums of current user
        public static List<AlbumDTO> ListAlbumsOfCurrentUser(long currentUserId, int page, int size)
        {
            using (var context = new CF_FamsamEntities())
            {
                var user = context.User.FirstOrDefault(u => u.id == currentUserId);
                var albumList = user.CreatedPost.Where(c => c.postType.Equals(GeneralPost.ALBUM_POST_TYPE)).Skip((page - 1) * size).Take(size).ToList();
                AlbumDTO albumInstance = new AlbumDTO();
                List<AlbumDTO> listAlbumDTO = new List<AlbumDTO>();
                foreach (var item in albumList)
                {
                    albumInstance.Id = item.Id;
                    albumInstance.Title = item.Album.title;
                    albumInstance.Description = item.description;
                    albumInstance.LastUpdate = item.lastUpdate;
                    albumInstance.AuthorFirstname = item.CreateUser.firstname;
                    albumInstance.AuthorLastname = item.CreateUser.lastname;
                    albumInstance.AuthorEmail = item.CreateUser.email;
                    albumInstance.ListPhoto = PhotoDAO.ListPhotosByAlbum(currentUserId, page, size, item.Id);

                    listAlbumDTO.Add(albumInstance);
                }

                return (listAlbumDTO.Count() > 0) ? listAlbumDTO : new List<AlbumDTO>(0);
            }
        }
Ejemplo n.º 5
0
 public static FamsamPrinciple CheckLogin(string email, string password)
 {
     using (var db = new CF_FamsamEntities())
     {
         User user = (from u in db.User
                      where u.email == email && u.password == password
                      select u).FirstOrDefault();
         if (user != null)
         {
             //TODO - create session
             return new FamsamPrinciple(new FamsamIdentity(user.email), user.UserRole.rolename);
         }
     }
     return null;
 }
Ejemplo n.º 6
0
        // list all albums of a user
        // kho qua assign cho Nhut
        public static List<AlbumDTO> ListAlbumsOfOtherUser(long currentUserId, long userId, int page, int size)
        {
            using (var context = new CF_FamsamEntities())
            {
                var currentUser = context.User.FirstOrDefault(u => u.id == currentUserId);
                var user = context.User.FirstOrDefault(u => u.id == userId);
                var storyPostList = user.CreatedPost.Where(c => c.postType.Equals(GeneralPost.STORY_POST_TYPE)).Skip((page -1) * size).Take(size).ToList();
                AlbumDTO albumInstance = new AlbumDTO();
                List<AlbumDTO> listAlbumDTO = new List<AlbumDTO>();
                foreach (var item in storyPostList)
                {
                    bool check = false;
                    if (item.Story.privacy.Equals(Story.PUBLIC_PRIVACY))
                    {
                        check = true;
                    }
                    if (item.Story.privacy.Equals(Story.FAMILY_ONLY_PRIVACY) && CheckUsersInFamily(currentUserId, userId, item.Story.familyId))
                    {
                        check = true;
                    }
                    //if (item.Story.privacy.Equals(Story.NEIGHBOR_ONLY_PRIVACY) && (CheckUsersInFamily(currentUserId, userId, item.Story.familyId)))
                    //albumInstance.Id = item.Id;
                    //albumInstance.Title = item.Album.title;
                    //albumInstance.Description = item.description;
                    //albumInstance.LastUpdate = item.lastUpdate;
                    //albumInstance.AuthorFirstname = item.CreateUser.firstname;
                    //albumInstance.AuthorLastname = item.CreateUser.lastname;
                    //albumInstance.AuthorEmail = item.CreateUser.email;
                    //albumInstance.ListPhoto = PhotoDAO.ListPhotosByAlbum(userId, page, size, item.Id);

                    listAlbumDTO.Add(albumInstance);
                }

                return (listAlbumDTO.Count() > 0) ? listAlbumDTO : new List<AlbumDTO>(0);
            }
        }
Ejemplo n.º 7
0
 // check if current user and target user are in one family.
 public static bool CheckUsersInFamily(long userId, long otherUserId, long familyId)
 {
     using (var context = new CF_FamsamEntities())
     {
         var userFamilyRole = context.FamilyRole.FirstOrDefault(fr => fr.userId == userId && fr.familyId == familyId);
         var otherUserFamilyRole = context.FamilyRole.FirstOrDefault(fr => fr.userId == otherUserId && fr.familyId == familyId);
         if (userFamilyRole != null && otherUserFamilyRole != null)
         {
             return true;
         }
         return false;
     }
 }
Ejemplo n.º 8
0
 // current user remove an album
 // return -1 if fail
 // return 0 if success
 public static int RemoveAlbum(long albumId, bool agreeToRemove)
 {
     using (var context = new CF_FamsamEntities())
     {
         var album = context.Album.FirstOrDefault(a => a.id == albumId);
         if (agreeToRemove)
         {
             try
             {
                 var listPhoto = album.Photo;
                 album.Photo.Clear();
                 foreach(var photo in listPhoto) {
                     context.Photo.Remove(photo);
                     context.GeneralPost.Remove(photo.Post);
                 }
                 context.Album.Remove(album);
                 context.GeneralPost.Remove(album.Post);
                 context.SaveChanges();
             }
             catch (Exception ex)
             {
                 Debug.WriteLine("Exception: " + ex.StackTrace);
                 return -1;
             }
         }
         else
         {
             try
             {
                 album.Photo.Clear();
                 context.Album.Remove(album);
                 context.GeneralPost.Remove(album.Post);
                 context.SaveChanges();
             }
             catch (Exception ex)
             {
                 Debug.WriteLine("Exception: " + ex.StackTrace);
                 return -1;
             }
         }
         return 0;
     }
 }
Ejemplo n.º 9
0
 // current user update album title
 // return -1 if fail
 // return 0 if success
 public static int EditAlbum(AlbumDTO albumEdit, List<PhotoDTO> listPhotoAdd, List<PhotoDTO> listPhotoRemove)
 {
     using (var context = new CF_FamsamEntities())
     {
         var post = context.GeneralPost.FirstOrDefault(p => p.Id == albumEdit.Id);
         var album = post.Album;
         post.lastUpdate = DateTime.Now;
         album.title = albumEdit.Title;
         post.description = albumEdit.Description;
         try
         {
             // add new list of photos to album
             if (listPhotoAdd.Count > 0)
             {
                 foreach (var newPhoto in listPhotoAdd)
                 {
                     album.Photo.Add(context.Photo.FirstOrDefault(p => p.id == newPhoto.Id));
                 }
             }
             // remove list of photos from album
             if (listPhotoRemove.Count > 0)
             {
                 foreach (var removePhoto in listPhotoRemove)
                 {
                     album.Photo.Remove(context.Photo.FirstOrDefault(p => p.id == removePhoto.Id));
                 }
             }
             context.Entry<Album>(album).State = EntityState.Modified;
             context.Entry<GeneralPost>(post).State = EntityState.Modified;
             context.SaveChanges();
         }
         catch (Exception ex)
         {
             Debug.WriteLine("Exception: " + ex.StackTrace);
             return -1;
         }
         return 0;
     }
 }
Ejemplo n.º 10
0
        /// <summary>
        /// Update photo
        /// </summary>
        /// <param name="photo"></param>
        /// <returns></returns>
        public static int EditDescription(PhotoDTO photoDTO)
        {


            using (var db = new CF_FamsamEntities())
            {
                Photo photo = db.Photo.Find(photoDTO.Id);
                if (photo == null) return -1;

                //update photo
                DateTime lastUpdate = DateTime.Now;
                photo.Post.lastUpdate = lastUpdate;
                photo.Post.description = photoDTO.Description;
                try
                {
                    db.Entry(photo).State = EntityState.Modified;
                    db.SaveChanges();
                    return 1;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Exception on Edit photo description:" + ex.ToString());
                }
            }
            return 0;
        }
Ejemplo n.º 11
0
 // 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;
     }
 }
Ejemplo n.º 12
0
        // get album by albumid
        public static AlbumDTO GetAlbumById(long userId, int page, int size, long albumId)
        {
            using (var context = new CF_FamsamEntities())
            {
                var generalPost = context.GeneralPost.FirstOrDefault(g => g.Id == albumId);
                AlbumDTO albumInstance = new AlbumDTO();

                albumInstance.Id = generalPost.Id;
                albumInstance.Title = generalPost.Album.title;
                albumInstance.Description = generalPost.description;
                albumInstance.LastUpdate = generalPost.lastUpdate;
                albumInstance.AuthorFirstname = generalPost.CreateUser.firstname;
                albumInstance.AuthorLastname = generalPost.CreateUser.lastname;
                albumInstance.AuthorEmail = generalPost.CreateUser.email;
                albumInstance.ListPhoto = PhotoDAO.ListPhotosByAlbum(userId, page, size, generalPost.Id);

                return albumInstance;
            }
        }
Ejemplo n.º 13
0
 /// <summary>
 /// List all photos by album
 /// </summary>
 /// <param name="userId">Id of user</param>
 /// <param name="page"></param>
 /// <param name="size"></param>
 /// <param name="albumId"></param>
 /// <returns>List of photos (0 item at least)</returns>
 public static List<PhotoDTO> ListPhotosByAlbum(long userId, int page, int size, long albumId)
 {
     List<GeneralPost> photos = new List<GeneralPost>();
     List<PhotoDTO> result = new List<PhotoDTO>();
     using (var db = new CF_FamsamEntities())
     {
         var albumQuery = from p in db.GeneralPost
                          where p.postType == GeneralPost.ALBUM_POST_TYPE && p.Id == albumId
                          orderby p.lastUpdate descending
                          select p;
         GeneralPost post = albumQuery.FirstOrDefault<GeneralPost>();
         if (post == null) return result;
         
         //to dto
         foreach (Photo photo in post.Album.Photo)
         {
             PhotoDTO photoDTO = new PhotoDTO();
             GeneralPost photoPost = photo.Post;
             photoDTO.Id = post.Id;
             photoDTO.AuthorFirstName = photoPost.CreateUser.firstname;
             photoDTO.AuthorLastName = photoPost.CreateUser.lastname;
             photoDTO.LastUpdate = photoPost.lastUpdate;
             photoDTO.Description = photoPost.description;
             photoDTO.ImageURL = photo.url;
             photoDTO.BadQuality = photo.badQuality;
             result.Add(photoDTO);
         }
     }
     return result;
 }
Ejemplo n.º 14
0
        public static void DeletePhoto(long photoId)
        {
            using (var db = new CF_FamsamEntities())
            {
                try
                {
                    Photo photo = db.Photo.Find(photoId);
                    if (photo != null) db.Photo.Remove(photo);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("exception on delete photo:" + ex);
                }
            }

        }
Ejemplo n.º 15
0
 public static PhotoDTO GetPhoto(long photoId)
 {
     using (var db = new CF_FamsamEntities())
     {
         Photo photo = db.Photo.Find(photoId);
         PhotoDTO photoDTO = new PhotoDTO();
         photoDTO.Id = photo.id;
         GeneralPost post = photo.Post;
         User user = post.CreateUser;
         photoDTO.AuthorId = user.id;
         photoDTO.AuthorEmail = user.email;
         photoDTO.LastUpdate = post.lastUpdate;
         photoDTO.Description = post.description;
         photoDTO.ImageURL = photo.url;
         photoDTO.BadQuality = photo.badQuality;
         photoDTO.tags = post.GetTagNameArray();
         return photoDTO;
     }
 }
Ejemplo n.º 16
0
        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);
                }
            }

        }
Ejemplo n.º 17
0
        public static long Authentication(String token){
            using (var db = new CF_FamsamEntities())
            {
                    Session session = db.Session.Find(token);
                    if (session == null) return -403;

                    if (session.expired < DateTime.Now)
                    {
                        Debug.WriteLine("____________________________ session mili:" + session.expired.Millisecond);
                        Debug.WriteLine("____________________________ now mili:" + DateTime.Now.Millisecond);
                        //session expired
                        db.Session.Remove(session);
                        db.SaveChanges();
                        return -403;
                    }
                    else
                    {
                        return session.User.id;
                    }

            }
        }
Ejemplo n.º 18
0
 // current user update album title
 // return -1 if fail
 // return 0 if success
 public static int EditAlbumTitle(AlbumDTO albumEdit)
 {
     using (var context = new CF_FamsamEntities())
     {
         var post = context.GeneralPost.FirstOrDefault(p => p.Id == albumEdit.Id);
         post.lastUpdate = DateTime.Now;
         var album = context.Album.FirstOrDefault(a => a.id == albumEdit.Id);
         album.title = albumEdit.Title;
         try
         {
             context.Entry<Album>(album).State = EntityState.Modified;
             context.Entry<GeneralPost>(post).State = EntityState.Modified;
             context.SaveChanges();
         }
         catch (Exception ex)
         {
             Debug.WriteLine("Exception: " + ex.StackTrace);
             return -1;
         }
         return 0;
     }
 }
Ejemplo n.º 19
0
        /// <summary>
        /// Authorization request header from client.
        /// </summary>
        /// <param name="header">header from client</param>
        /// <returns>-401/-403/{userId}</returns>
        public static long Authentication(HttpRequestHeaders header)
        {
            string authorization = header.GetValues("Authorization").FirstOrDefault();
            if (authorization == null)
                {
                    return -401;
                }
            using (var db = new CF_FamsamEntities())
            {
                string token = authorization.Split(null)[1];
                    Session session = db.Session.Find(token);
                    Debug.WriteLine("____________________________" + session.token);
                    if (session == null) return -403;

                    if (session.expired < DateTime.Now)
                    {
                        Debug.WriteLine("____________________________ session mili:" + session.expired.Millisecond);
                        Debug.WriteLine("____________________________ now mili:" + DateTime.Now.Millisecond);
                        //session expired
                        db.Session.Remove(session);
                        db.SaveChanges();
                        return -403;
                    }
                    else
                    {
                        return session.User.id;
                    }


                

            }
        }
Ejemplo n.º 20
0
        /// <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;
            }

        }