Skip to content

Instantly share code, notes, and snippets.

@oguzhancagliyan
Last active September 30, 2020 00:18
Show Gist options
  • Save oguzhancagliyan/c53eb1e08ec38191edbbf28b0e9440f6 to your computer and use it in GitHub Desktop.
Save oguzhancagliyan/c53eb1e08ec38191edbbf28b0e9440f6 to your computer and use it in GitHub Desktop.
public class MovieService : IMovieService
{
private readonly IMovieRepository _movieRepository;
private readonly IValidatorService _validatorService;
private readonly IAmazonSQS _amazonSqs;
private readonly SqsQueueConfig _sqsQueueConfig;
public MovieService(IMovieRepository movieRepository, IValidatorService validatorService, IAmazonSQS amazonSqs, IOptions<SqsQueueConfig> options)
{
_movieRepository = movieRepository;
_validatorService = validatorService;
_amazonSqs = amazonSqs;
_sqsQueueConfig = options.Value;
}
public async Task<GetMovieResponseModel> GetMovieByIdAsync(Guid movieId, CancellationToken token)
{
Contract.Requires<Exception>(movieId != Guid.Empty, nameof(movieId));
MovieEntity movieEntity = await _movieRepository.GetMovieByIdAsync(movieId, token);
GetMovieResponseModel responseModel = await movieEntity.BuildAdapter().AdaptToTypeAsync<GetMovieResponseModel>();
return responseModel;
}
public async Task<AddMovieResponseModel> AddMovieAsync(AddMovieRequestModel model, CancellationToken token = default)
{
try
{
await _validatorService.ValidationCheck<AddMovieRequestModelValidator, AddMovieRequestModel>(model);
MovieEntity entity = await model.BuildAdapter().AdaptToTypeAsync<MovieEntity>();
await _movieRepository.AddAsync(entity, token);
AddMovieResponseModel responseModel = await entity.BuildAdapter().AdaptToTypeAsync<AddMovieResponseModel>();
return responseModel;
}
catch (Exception)
{
return null;
}
}
public async Task<bool> CommentMovie(CommentModel model, CancellationToken token = default)
{
try
{
await _validatorService.ValidationCheck<CommentModelValidator, CommentModel>(model);
string serializedObject = JsonSerializer.Serialize(model);
string queueName = _sqsQueueConfig.QueueName;
GetQueueUrlResponse response = await _amazonSqs.GetQueueUrlAsync(queueName, token);
var sendMessageRequest = new SendMessageRequest
{
QueueUrl = response.QueueUrl,
MessageGroupId = model.MovieId.ToString(),
MessageDeduplicationId = Guid.NewGuid().ToString(),
MessageBody = serializedObject
};
var messageResponse = await _amazonSqs.SendMessageAsync(sendMessageRequest, token);
if (messageResponse.HttpStatusCode != HttpStatusCode.OK)
{
return false;
}
return true;
}
catch (Exception e)
{
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment