Example #1
0
        /* public members */
        public static void CreateUser(string username, string firstName, string lastName, string authCode)
        {
            ValidateUsername(username);
            ValidateName(firstName);
            ValidateName(lastName);
            ValidateAuthCode(authCode);
            using (GalleryContext context = new GalleryContext())
            {
                var usernameToLower = username.ToLower();
                var firstNameToLower = firstName.ToLower();
                var lastNameToLower = lastName.ToLower();

                var dbUser = context.Users.FirstOrDefault(u => u.Username.ToLower() == usernameToLower);

                if (dbUser != null)
                {
                    if (dbUser.Username.ToLower() == usernameToLower)
                    {
                        throw new ServerErrorException("Username already exists", "ERR_DUP_USR");
                    }
                }

                dbUser = new User()
                {
                    Username = usernameToLower,
                    FirstName = firstName,
                    LastName = lastName,
                    AuthCode = authCode
                };
                context.Users.Add(dbUser);
                context.SaveChanges();
            }
        }
Example #2
0
        internal static IEnumerable<GalleryModel> GetAll(string sessionKey)
        {
            var context = new GalleryContext();
            var result = context.Galleries.Select(GalleryModel.FromGallery).ToList();

            return result;
        }
Example #3
0
        internal static IEnumerable<AlbumPreviewModel> GetAllAlbums()
        {
            var context = new GalleryContext();
            var result = context.Albums.Select(AlbumPreviewModel.FromAlbum).ToList();

            return result;
        }
Example #4
0
        public static void CreateAlbum(AlbumModel album, int userID)
        {
            ValidateAlbumName(album.Title);

            using (GalleryContext context = new GalleryContext())
            {
                var albumNameToLower = album.Title.ToLower();

                //var dbUser = context.Users.FirstOrDefault(u => u.Username.ToLower() == usernameToLower);
                var dbAlbum = context.Albums.FirstOrDefault(a => a.Title.ToLower() == albumNameToLower);
                if (dbAlbum != null)
                {
                    if (dbAlbum.Title.ToLower() == albumNameToLower)
                    {
                        throw new ServerErrorException("Album already exists", "ERR_DUP_USR");
                    }
                }

                dbAlbum = new Album()
                {
                    Title = albumNameToLower,
                    DateCreated = album.DateCreated,
                    DateModified = album.DateModified
                };

                context.Users.Find(userID).Galleries.First().Albums.Add(dbAlbum);
                context.Albums.Add(dbAlbum);
                context.SaveChanges();
            }
        }
Example #5
0
 protected static User GetUser(int userId, GalleryContext context)
 {
     var user = context.Users.FirstOrDefault(u => u.ID == userId);
     if (user == null)
     {
         throw new ServerErrorException("Invalid user", "ERR_INV_USR");
     }
     return user;
 }
Example #6
0
        public static void CreateGallery(string name, string sessionKey)
        {
            using (GalleryContext context = new GalleryContext())
            {
                var dbGallery = new Gallery.Models.Gallery()
                {
                    Name = name
                };

                var userId = context.Users.Where(u => u.SessionKey == sessionKey).FirstOrDefault().ID;
                context.Users.Find(userId).Galleries.Add(dbGallery);
                context.SaveChanges();
            }
        }
Example #7
0
        public static void CreateComment(int? imageId, string text, int userId)
        {
            using (GalleryContext context = new GalleryContext())
            {
                var author = context.Users.Find(userId);
                var image = context.Images.Find(imageId);
                var dbComment = new Gallery.Models.Comment()
                {
                    Text=text,
                    Author=author,
                    Image = image
                };

                context.Comments.Add(dbComment);
                context.SaveChanges();
            }
        }
Example #8
0
        internal static IEnumerable<CommentFullModel> GetAll(int imageID, int userID)
        {
            using (var context = new GalleryContext())
            {
                var comments = context.Comments.Where(c => c.Image.ID == imageID);
                var result =
                    (from c in comments
                     select new CommentFullModel
                     {
                         Text = c.Text,
                         Author = new UserModel
                         {
                             ID = c.Author.ID,
                             Username = c.Author.Username
                         }
                     }).ToList();

                return result;
            }
        }
Example #9
0
        internal static AlbumFullModel GetAlbumByID(int albumID, int userID)
        {
            using (var context = new GalleryContext())
            {
                var images = context.Albums.FirstOrDefault(a => a.ID == albumID);

                return new AlbumFullModel
                {
                    Title = images.Title,
                    Images =
                        (from i in images.Images
                        select new ImageModel
                        {
                            ID=i.ID,
                            Title=i.Title,
                            Url=i.Url
                        }).ToList()
                    };
                };
        }
Example #10
0
        public static string LoginUser(string username, string authCode, out string firstName, out string lastName)
        {
            ValidateUsername(username);
            ValidateAuthCode(authCode);
            var context = new GalleryContext();
            using (context)
            {
                var usernameToLower = username.ToLower();
                var user = context.Users.FirstOrDefault(u => u.Username.ToLower() == usernameToLower && u.AuthCode == authCode);
                if (user == null)
                {
                    throw new ServerErrorException("Invalid user authentication", "INV_USR_AUTH");
                }

                var sessionKey = GenerateSessionKey((int)user.ID);
                user.SessionKey = sessionKey;
                firstName = user.FirstName;
                lastName = user.LastName;
                context.SaveChanges();
                return sessionKey;
            }
        }
Example #11
0
        internal static GalleryFullModel GetSingle(string username)
        {
            var context = new GalleryContext();
            using (context)
            {
                var gallery =
                    from g in context.Galleries.Include("User").Include("Albums").Include("Images")
                    where g.User.Username == username
                    select new GalleryFullModel
                    {
                        ID = g.ID,
                        Name = g.Name,
                        Albums =
                            from album in g.Albums
                            where g.User.Username == username
                            select new AlbumModel
                            {
                                DateCreated = album.DateCreated,
                                DateModified = album.DateModified,
                                ID = album.ID,
                                Title = album.Title
                            },
                        Images =
                            from i in g.Images
                            where g.User.Username == username
                            select new ImageModel
                            {
                                DateUploaded = i.DateUploaded,
                                ID = i.ID,
                                Title = i.Title,
                                Url = i.Url
                            }
                    };

                return gallery.FirstOrDefault();
            }
        }
Example #12
0
 public static int LoginUser(string sessionKey)
 {
     ValidateSessionKey(sessionKey);
     var context = new GalleryContext();
     using (context)
     {
         var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
         if (user == null)
         {
             throw new ServerErrorException("Invalid user authentication", "INV_USR_AUTH");
         }
         return (int)user.ID;
     }
 }
Example #13
0
        //public static IEnumerable<UserScore> GetAllUsers()
        //{
        //    var context = new BullsAndCowsEntities();
        //    using (context)
        //    {
        //        var users =
        //            (from user in context.Users
        //             select new UserScore()
        //             {
        //                 Nickname = user.Nickname,
        //                 Score = user.Scores.Any() ? user.Scores.Sum(sc => sc.Value) : 0
        //             });
        //        return users.ToList();
        //    }
        //}
        internal static IEnumerable<UserPreviewModel> GetAllUsers()
        {
            var context = new GalleryContext();
            var result = context.Users.Select(UserPreviewModel.FromUser).ToList();

            return result;
        }
Example #14
0
 public static void LogoutUser(string sessionKey)
 {
     ValidateSessionKey(sessionKey);
     var context = new GalleryContext();
     using (context)
     {
         var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
         if (user == null)
         {
             throw new ServerErrorException("Invalid user authentication", "INV_USR_AUTH");
         }
         user.SessionKey = null;
         context.SaveChanges();
     }
 }