コード例 #1
0
        public HttpResponseMessage PostPicture(string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions<HttpResponseMessage>(() =>
            {
                UsersController.ValidateSessionKey(sessionKey);
                var files = HttpContext.Current.Request.Files;

                if (files.Count > 0)
                {
                    for (int i = 0; i < files.Count; i++)
                    {
                        var postedFile = files[i];
                        var filePath = HttpContext.Current.Server.MapPath(Path.GetTempPath() + postedFile.FileName);
                        postedFile.SaveAs(filePath);

                        using (var context = new PictureTogetherContext())
                        {
                            context.Albums.First().Pictures.Add(
                                new Picture
                                {
                                    Url = filePath
                                });
                            context.SaveChanges();
                        }
                    }

                    return this.Request.CreateResponse(HttpStatusCode.Created);
                }

                return this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "No file received from the server.");
            });

            return responseMsg;
        }
コード例 #2
0
        public HttpResponseMessage PostRegister(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions<HttpResponseMessage>(
                () =>
                {
                    using (var context = new PictureTogetherContext())
                    {
                        this.ValidateUsername(model.Username);
                        this.ValidateAuthCode(model.AuthCode);

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

                        if (user != null)
                        {
                            throw new InvalidOperationException("User already exists.");
                        }

                        user = new User
                        {
                            Username = model.Username,
                            AuthCode = model.AuthCode
                        };

                        context.Users.Add(user);
                        context.SaveChanges();

                        user.SessionKey = this.GenerateSessionKey(user.Id);
                        context.SaveChanges();

                        var loggedModel = new LoggedUserModel
                        {
                            Username = user.Username,
                            SessionKey = user.SessionKey
                        };

                        var response =
                            this.Request.CreateResponse(HttpStatusCode.Created,
                                loggedModel);
                        return response;
                    }
                });

            return responseMsg;
        }
コード例 #3
0
        public HttpResponseMessage PostLogin(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions<HttpResponseMessage>(
                () =>
                {
                    using (var context = new PictureTogetherContext())
                    {
                        this.ValidateUsername(model.Username);
                        this.ValidateAuthCode(model.AuthCode);

                        var usernameToLower = model.Username.ToLower();
                        var user = context.Users.FirstOrDefault(
                            usr => usr.Username == usernameToLower &&
                                   usr.AuthCode == model.AuthCode);

                        if (user == null)
                        {
                            throw new InvalidOperationException("Wrong username or password.");
                        }

                        if (user.SessionKey == null)
                        {
                            user.SessionKey = this.GenerateSessionKey(user.Id);
                            context.SaveChanges();
                        }

                        var loggedModel = new LoggedUserModel
                        {
                            Username = user.Username,
                            SessionKey = user.SessionKey,
                            Albums = user.Albums.Select(a => new AlbumModel
                            {
                                Id = a.Id,
                                Name = a.Name
                            }).ToList()
                        };

                        var response =
                            this.Request.CreateResponse(HttpStatusCode.Created,
                                loggedModel);
                        return response;
                    }
                });

            return responseMsg;
        }
コード例 #4
0
        public HttpResponseMessage PutLogout(string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions<HttpResponseMessage>(
                () =>
                {
                    using (var context = new PictureTogetherContext())
                    {
                        ValidateSessionKey(sessionKey);

                        var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                        if (user != null)
                        {
                            user.SessionKey = null;
                            context.SaveChanges();
                        }

                        var response = new HttpResponseMessage(HttpStatusCode.OK);
                        return response;
                    }
                });

            return responseMsg;
        }
コード例 #5
0
        public HttpResponseMessage PostAlbum(string sessionKey, AlbumFullModel albumFullModel)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions<HttpResponseMessage>(() =>
            {
                using (var context = new PictureTogetherContext())
                {
                    UsersController.ValidateSessionKey(sessionKey);
                    var currentUser = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                    if (currentUser == null)
                    {
                        throw new ArgumentException("Expired or invalid sessionKey. Please try to relog with your account.");
                    }

                    var newAlbum = new Album
                    {
                        Name = albumFullModel.Name,
                        Latitude = albumFullModel.Latitude,
                        Longitude = albumFullModel.Longitude,
                        Users =
                        {
                            currentUser
                        }
                    };

                    currentUser.Albums.Add(newAlbum);
                    context.Albums.Add(newAlbum);
                    context.SaveChanges();

                    var albumModel = new AlbumModel
                    {
                        Id = newAlbum.Id,
                        Name = newAlbum.Name
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.Created, albumModel);
                    return response;
                }
            });

            return responseMsg;
        }
コード例 #6
0
        public HttpResponseMessage PutAlbum(string sessionKey, ShareCodeModel shareCodeModel)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions<HttpResponseMessage>(() =>
            {
                using (var context = new PictureTogetherContext())
                {
                    UsersController.ValidateSessionKey(sessionKey);
                    var currentUser = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                    if (currentUser == null)
                    {
                        throw new ArgumentException("Expired or invalid sessionKey. Please try to relog with your account.");
                    }

                    var album = context.Albums.Find(shareCodeModel.AlbumId);
                    if (album == null)
                    {
                        throw new ArgumentException("Invalid share code.");
                    }

                    var sharingUser = context.Users.FirstOrDefault(u => u.Username == shareCodeModel.Username);
                    if (sharingUser == null)
                    {
                        throw new ArgumentException("Invalid share code.");
                    }

                    var sharingUserAlbum = sharingUser.Albums.FirstOrDefault(a => a.Id == album.Id);
                    if (sharingUserAlbum == null)
                    {
                        throw new ArgumentException("Invalid share code.");
                    }

                    var currentUserAlbum = currentUser.Albums.FirstOrDefault(a => a.Id == album.Id);
                    if (currentUserAlbum != null)
                    {
                        throw new ArgumentException("This album is already shared with you.");
                    }

                    currentUser.Albums.Add(album);
                    album.Users.Add(currentUser);
                    context.SaveChanges();

                    var albumModel = new AlbumModel
                    {
                        Id = album.Id,
                        Name = album.Name
                    };
                    var response = this.Request.CreateResponse(HttpStatusCode.OK, albumModel);
                    return response;
                }
            });

            return responseMsg;
        }