示例#1
0
        public async Task <IActionResult> Post([FromBody] TweetsTbl value)
        {
            // new id is number of rows + 1
            long id;

            try
            {
                id = await dbContext.TweetsTbl.MaxAsync(tweet => tweet.Id) + 1;
            }
            catch (Exception)
            {
                id = 1;
            }

            UsersTbl user;

            try
            {
                user = await dbContext.UsersTbl.Where(u => u.UserName.Equals(value.UserName)).FirstAsync();
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, ex.Message));
            }

            TweetsTbl tweet = new TweetsTbl();

            tweet.Id             = id;
            tweet.UserName       = value.UserName;
            tweet.ProfilePicPath = user.ProfilePicPath;
            tweet.Tweet          = value.Tweet;
            tweet.Imagepath      = value.Imagepath;
            tweet.CommentsCount  = 0;
            tweet.LikesCount     = 0;
            tweet.SharesCount    = 0;

            //add to DB
            try
            {
                await dbContext.AddAsync(tweet);

                await dbContext.SaveChangesAsync();

                return(StatusCode(StatusCodes.Status200OK, "Tweet Posted"));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, ex.Message));
            }
        }
        public async Task <IActionResult> Post([FromQuery] long tweet_id)
        {
            try
            {
                TweetsTbl tweet = await dbContext.TweetsTbl.Where(t => t.Id == tweet_id).FirstAsync();

                tweet.SharesCount++;
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status404NotFound));
            }

            try
            {
                await dbContext.SaveChangesAsync();

                return(StatusCode(StatusCodes.Status200OK, "Tweet Shared"));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status400BadRequest, ex.Message));
            }
        }