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;
|
|
|
|
|
using Retroactiune.Models;
|
|
|
|
|
using Retroactiune.Settings;
|
|
|
|
|
|
|
|
|
|
namespace Retroactiune.Services
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Service that simplifies access to the database for managing FeedbackReceiver items.
|
|
|
|
|
/// <see cref="FeedbackReceiver"/>
|
|
|
|
|
/// </summary>
|
2021-05-29 18:12:36 +00:00
|
|
|
|
public class FeedbackReceiverService : IFeedbackReceiverService
|
2021-05-29 17:02:16 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly IMongoCollection<FeedbackReceiver> _collection;
|
|
|
|
|
|
2021-05-30 18:16:01 +00:00
|
|
|
|
public FeedbackReceiverService(IMongoClient client, IMongoDbSettings settings)
|
2021-05-29 17:02:16 +00:00
|
|
|
|
{
|
|
|
|
|
var database = client.GetDatabase(settings.DatabaseName);
|
|
|
|
|
_collection = database.GetCollection<FeedbackReceiver>(settings.FeedbackReceiverCollectionName);
|
|
|
|
|
}
|
|
|
|
|
|
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));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!items.Any())
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
await _collection.InsertManyAsync(items);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
throw new GenericServiceException($"Operation failed: {e.Message}");
|
|
|
|
|
}
|
2021-06-01 13:20:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task DeleteOneAsync(string guid)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var filter = new FilterDefinitionBuilder<FeedbackReceiver>();
|
|
|
|
|
await _collection.DeleteOneAsync(filter.Eq(i => i.Id, guid));
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
throw new GenericServiceException($"Operation failed: {e.Message}");
|
|
|
|
|
}
|
2021-05-29 17:02:16 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|