public async Task <Result <SpotPutResponse> > UpdateSpot(long id, SpotPutRequest request) { if (!IsValidId(id)) { return(Error <SpotPutResponse>(Errors.SpotIdIsInvalid)); } var validate = await request.ValidateAsync(); if (!validate.IsValid) { var error = validate.Errors.FirstOrDefault(); return(Error <SpotPutResponse>(error.ToMetaError())); } Spot spot; try { spot = await _repository.Read(id); } catch { return(Error <SpotPutResponse>(Errors.SpotReadingFromDatabaseError)); } if (spot == null) { return(Error <SpotPutResponse>(Errors.SpotNotFoundError)); } long facebookId = 0; long twitterId = 0; try { facebookId = await _facebookClient.CreatePost(spot.Message); twitterId = await _twitterClient.PublishTweet(spot.Message); } catch (FacebookClientException) { if (twitterId != 0) { await _twitterClient.DestroyTweet(twitterId); } return(Error <SpotPutResponse>(Errors.SpotCreatingFacebookPostError)); } catch (TwitterClientException) { if (facebookId != 0) { await _facebookClient.DeletePost(facebookId); } return(Error <SpotPutResponse>(Errors.SpotPublishingTweetError)); }; spot.Status = request.Status.ToStatusEnum(); spot.FacebookId = facebookId; spot.TwitterId = twitterId; try { spot = await _repository.Update(spot); } catch { await _facebookClient.DeletePost(facebookId); await _twitterClient.DestroyTweet(twitterId); return(Error <SpotPutResponse>(Errors.SpotUpdatingOnDatabaseError)); } return(Success(spot.ToSpotPutResponse())); }