2021-05-29 17:02:16 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-06-01 11:37:00 +00:00
|
|
|
|
using System.Linq;
|
2021-05-29 17:02:16 +00:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using MongoDB.Driver;
|
2021-06-21 14:46:44 +00:00
|
|
|
|
using Retroactiune.Core.Entities;
|
|
|
|
|
using Retroactiune.Core.Interfaces;
|
2021-05-29 17:02:16 +00:00
|
|
|
|
|
2021-06-21 14:46:44 +00:00
|
|
|
|
namespace Retroactiune.Core.Services
|
2021-05-29 17:02:16 +00:00
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Service that simplifies access to the database for managing FeedbackReceiver items.
|
|
|
|
|
/// <see cref="FeedbackReceiver"/>
|
|
|
|
|
/// </summary>
|
2021-07-04 14:39:07 +00:00
|
|
|
|
public class FeedbackReceiversService : IFeedbackReceiversService
|
2021-05-29 17:02:16 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IMongoCollection<FeedbackReceiver> _collection;
|
|
|
|
|
|
2021-07-04 14:39:07 +00:00
|
|
|
|
public FeedbackReceiversService(IMongoClient client, IDatabaseSettings settings)
|
2021-05-29 17:02:16 +00:00
|
|
|
|
{
|
|
|
|
|
var database = client.GetDatabase(settings.DatabaseName);
|
2021-07-11 13:26:01 +00:00
|
|
|
|
_collection = database.GetCollection<FeedbackReceiver>(settings.FeedbackReceiversCollectionName);
|
2021-05-29 17:02:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-29 18:12:36 +00:00
|
|
|
|
public async Task CreateManyAsync(IEnumerable<FeedbackReceiver> items)
|
2021-05-29 17:02:16 +00:00
|
|
|
|
{
|
2021-06-01 11:37:00 +00:00
|
|
|
|
if (items == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(items));
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-05 14:47:22 +00:00
|
|
|
|
var feedbackReceivers = items as FeedbackReceiver[] ?? items.ToArray();
|
|
|
|
|
if (!feedbackReceivers.Any())
|
2021-06-01 11:37:00 +00:00
|
|
|
|
{
|
|
|
|
|
throw new GenericServiceException("items must contain at least one element");
|
|
|
|
|
}
|
2021-06-01 13:20:31 +00:00
|
|
|
|
|
2021-05-29 17:02:16 +00:00
|
|
|
|
try
|
|
|
|
|
{
|
2021-06-05 14:47:22 +00:00
|
|
|
|
await _collection.InsertManyAsync(feedbackReceivers);
|
2021-05-29 17:02:16 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2021-06-05 14:47:22 +00:00
|
|
|
|
throw new GenericServiceException($"Operation failed: {e.Message} {e.StackTrace}");
|
2021-05-29 17:02:16 +00:00
|
|
|
|
}
|
2021-06-01 13:20:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-20 11:25:04 +00:00
|
|
|
|
public async Task DeleteManyAsync(IEnumerable<string> guids)
|
2021-06-01 13:20:31 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var filter = new FilterDefinitionBuilder<FeedbackReceiver>();
|
2021-06-20 11:25:04 +00:00
|
|
|
|
await _collection.DeleteManyAsync(filter.In(i => i.Id, guids));
|
2021-06-01 13:20:31 +00:00
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2021-06-05 14:47:22 +00:00
|
|
|
|
throw new GenericServiceException($"Operation failed: {e.Message} {e.StackTrace}");
|
2021-06-01 13:20:31 +00:00
|
|
|
|
}
|
2021-05-29 17:02:16 +00:00
|
|
|
|
}
|
2021-06-05 13:03:12 +00:00
|
|
|
|
|
2021-06-05 16:39:47 +00:00
|
|
|
|
public async Task<IEnumerable<FeedbackReceiver>> FindAsync(IEnumerable<string> guids, int? offset = null,
|
|
|
|
|
int? limit = null)
|
2021-06-05 13:03:12 +00:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-06-05 16:39:47 +00:00
|
|
|
|
var guidsArr = guids as string[] ?? guids.ToArray();
|
|
|
|
|
|
|
|
|
|
var filterBuilder = new FilterDefinitionBuilder<FeedbackReceiver>();
|
|
|
|
|
var filterOptions = new FindOptions<FeedbackReceiver, FeedbackReceiver>();
|
|
|
|
|
var filter = filterBuilder.Empty;
|
|
|
|
|
|
|
|
|
|
// Filter for guids
|
|
|
|
|
if (guidsArr.Any())
|
|
|
|
|
{
|
|
|
|
|
filter = filterBuilder.In(i => i.Id, guidsArr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set skip
|
|
|
|
|
if (offset != null)
|
|
|
|
|
{
|
|
|
|
|
filterOptions.Skip = offset;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set limit
|
|
|
|
|
if (limit != null)
|
|
|
|
|
{
|
|
|
|
|
filterOptions.Limit = limit;
|
|
|
|
|
}
|
2021-06-05 13:03:12 +00:00
|
|
|
|
|
2021-06-05 16:39:47 +00:00
|
|
|
|
var cursor = await _collection.FindAsync(filter, filterOptions);
|
2021-06-05 13:03:12 +00:00
|
|
|
|
return cursor.ToList();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2021-06-05 14:47:22 +00:00
|
|
|
|
throw new GenericServiceException($"Operation failed: {e.Message} {e.StackTrace}");
|
2021-06-05 13:03:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-29 17:02:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|