Пример #1
0
        public async Task <IActionResult> PostPost([FromBody] PostModel body, [FromHeader] string token)
        {
            await Db.Connection.OpenAsync();

            if (!body.IsValidPost() || !await threadQuery.ThreadExists((int)body.thread_id))
            {
                return(new NotFoundObjectResult("Invalid body"));
            }

            var tokenModel = await authorizationQuery.GetTokenModel(token);

            //check that user is logged in
            if (tokenModel != null)
            {
                //set post's user_id and location if applicable
                body.user_id = tokenModel.user_id;
                var userLocation = await locationQuery.GetLocation((int)body.user_id);

                if (userLocation != null)
                {
                    body.latitude  = userLocation.latitude;
                    body.longitude = userLocation.longitude;
                }

                //add post
                if (await postQuery.AddPost(body))
                {
                    return(new OkResult());
                }
                else
                {
                    return(new NotFoundObjectResult("Could not add post"));
                }
            }

            return(new NotFoundObjectResult("User must be logged in to create post"));
        }