Esempio n. 1
0
        public async Task <IActionResult> Delete(string id)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(StatusCode(StatusCodes.Status401Unauthorized, new ErrorResult {
                    Succeeded = false, Message = "Please login first"
                }));
            }

            Tweet tweet = null;

            try
            {
                tweet = await _tweetService.GetTweetAsync(id);

                if (tweet.UserId != User.Identity.Name)
                {
                    return(StatusCode(StatusCodes.Status401Unauthorized, new ErrorResult {
                        Succeeded = false, Message = "You are not authorized to delete this tweet"
                    }));
                }

                tweet = await _tweetService.DeleteAsync(id);

                if (tweet == null)
                {
                    return(StatusCode(StatusCodes.Status204NoContent, new ErrorResult {
                        Succeeded = false, Message = "Id not found, please check id again"
                    }));
                }
            }
            catch (MyException myex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, new ErrorResult {
                    Succeeded = false, Message = "Server Error"
                }));
            }
            return(Ok(_mapper.Map <Tweet, TweetDTO>(tweet)));
        }