Beispiel #1
0
        public async Task <IActionResult> PostLocation([FromBody] UserModel body, [FromHeader] string token)
        {
            await Db.Connection.OpenAsync();

            //return 404 code if latitude and longitude are not given
            if (body.latitude == null || body.longitude == null)
            {
                return(new NotFoundObjectResult("Invalid body"));
            }
            if (body.GetGeoCoordinates() == null)
            {
                return(new NotFoundObjectResult("Invalid coordinates"));
            }

            var userToken = await authorizationQuery.GetTokenModel(token);

            if (userToken != null)
            {
                body.user_id = userToken.user_id;
                if (await userQuery.AddUserLocation(body))
                {
                    return(new OkResult());
                }
                {
                    return(new NotFoundObjectResult("Server error"));
                }
            }

            return(new NotFoundObjectResult("User must be logged in to set location"));
        }
Beispiel #2
0
        //logout user using their authorization token
        public async Task <bool> Logout(string token)
        {
            await Db.Connection.ChangeDataBaseAsync("users");

            using (var cmd = Db.Connection.CreateCommand())
            {
                //get AuthorizationTokenModel from db and if it exists, delete is and return true
                var authorizationQuery = new AuthorizationQuery(Db);
                var authorizationToken = await authorizationQuery.GetTokenModel(token);

                if (authorizationToken != null)
                {
                    //attempt to delete token and return true if token is deleted
                    cmd.CommandText = "DELETE FROM authorization_tokens WHERE token = @token";
                    cmd.Parameters.AddWithValue("@token", token);
                    if (await cmd.ExecuteNonQueryAsync() > 0)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
        }
Beispiel #3
0
        public async Task <IActionResult> PostNewThread([FromBody] ThreadModel body, [FromHeader] string token)
        {
            if (!body.IsValidThread())
            {
                return(new NotFoundObjectResult("Invalid body"));
            }

            await Db.Connection.OpenAsync();

            //check that user is logged in
            if (await authorizationQuery.GetTokenModel(token) != null)
            {
                //return 200 if thread is successfully added
                if (await postQuery.AddThread(body))
                {
                    return(new OkResult());
                }

                return(new NotFoundObjectResult("Could not add thread"));
            }

            return(new NotFoundObjectResult("User must be logged in to add thread"));
        }