245 lines
6.8 KiB
C#
245 lines
6.8 KiB
C#
using Commentbase;
|
|
using MongoDB.Driver;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Runtime.ConstrainedExecution;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Database
|
|
{
|
|
public class ImplementsMongoDB : Abstracts
|
|
{
|
|
private IMongoDatabase _database;
|
|
private IMongoCollection<Album> _albumCollection;
|
|
private IMongoCollection<Location> _locationCollection;
|
|
private IMongoCollection<Author> _authorCollection;
|
|
private IMongoCollection<Photo> _photoCollection;
|
|
private IMongoCollection<Comment> _commentCollection;
|
|
private IMongoCollection<Sequence> _sequenceCollection;
|
|
|
|
public ImplementsMongoDB()
|
|
{
|
|
var client = new MongoClient("mongodb://localhost:27017");
|
|
_database = client.GetDatabase("SUBD");
|
|
|
|
_albumCollection = _database.GetCollection<Album>("Album");
|
|
_locationCollection = _database.GetCollection<Location>("Location");
|
|
_authorCollection = _database.GetCollection<Author>("Author");
|
|
_photoCollection = _database.GetCollection<Photo>("Photo");
|
|
_commentCollection = _database.GetCollection<Comment>("Comment");
|
|
_sequenceCollection = _database.GetCollection<Sequence>("sequence");
|
|
}
|
|
//Album
|
|
public override void CreateAlbum(Album album)
|
|
{
|
|
if (album.Id == 0)
|
|
{
|
|
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Album").FirstOrDefault();
|
|
sequence.Count++;
|
|
album.Id = sequence.Count;
|
|
_sequenceCollection.ReplaceOne(seq => seq.Id == "Album", sequence);
|
|
}
|
|
_albumCollection.InsertOne(album);
|
|
}
|
|
|
|
public override List<Album> GetAlbums()
|
|
{
|
|
return _albumCollection.Find(_ => true).ToList();
|
|
}
|
|
|
|
public override Album GetAlbum(int id)
|
|
{
|
|
return _albumCollection.Find(album => album.Id == id).FirstOrDefault();
|
|
}
|
|
|
|
public override void UpdateAlbum(Album album)
|
|
{
|
|
_albumCollection.ReplaceOne(c => c.Id == album.Id, album);
|
|
}
|
|
|
|
public override void DeleteAlbum(int id)
|
|
{
|
|
_albumCollection.DeleteOne(album => album.Id == id);
|
|
}
|
|
public override Album GetAlbum(string title)
|
|
{
|
|
return _albumCollection.Find(album => album.Title == title).FirstOrDefault();
|
|
}
|
|
public override void DeleteAllAlbums()
|
|
{
|
|
_albumCollection.DeleteManyAsync(Builders<Album>.Filter.Empty);
|
|
}
|
|
|
|
// Location
|
|
public override void CreateLocation(Location location)
|
|
{
|
|
if (location.Id == 0)
|
|
{
|
|
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Location").FirstOrDefault();
|
|
sequence.Count++;
|
|
location.Id = sequence.Count;
|
|
_sequenceCollection.ReplaceOne(seq => seq.Id == "Location", sequence);
|
|
}
|
|
_locationCollection.InsertOne(location);
|
|
}
|
|
|
|
public override List<Location> GetLocations()
|
|
{
|
|
return _locationCollection.Find(_ => true).ToList();
|
|
}
|
|
|
|
public override Location GetLocation(int id)
|
|
{
|
|
return _locationCollection.Find(location => location.Id == id).FirstOrDefault();
|
|
}
|
|
|
|
public override void UpdateLocation(Location location)
|
|
{
|
|
_locationCollection.ReplaceOne(c => c.Id == location.Id, location);
|
|
}
|
|
|
|
public override void DeleteLocation(int id)
|
|
{
|
|
_locationCollection.DeleteOne(location => location.Id == id);
|
|
}
|
|
public override Location GetLocation(string name)
|
|
{
|
|
return _locationCollection.Find(location => location.Name == name).FirstOrDefault();
|
|
}
|
|
public override void DeleteAllLocations()
|
|
{
|
|
_locationCollection.DeleteManyAsync(Builders<Location>.Filter.Empty);
|
|
}
|
|
|
|
// Author
|
|
public override void CreateAuthor(Author author)
|
|
{
|
|
if (author.Id == 0)
|
|
{
|
|
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Author").FirstOrDefault();
|
|
sequence.Count++;
|
|
author.Id = sequence.Count;
|
|
_sequenceCollection.ReplaceOne(seq => seq.Id == "Author", sequence);
|
|
}
|
|
_authorCollection.InsertOne(author);
|
|
}
|
|
|
|
public override List<Author> GetAuthors()
|
|
{
|
|
return _authorCollection.Find(_ => true).ToList();
|
|
}
|
|
|
|
public override Author GetAuthor(int id)
|
|
{
|
|
return _authorCollection.Find(author => author.Id == id).FirstOrDefault();
|
|
}
|
|
|
|
public override void UpdateAuthor(Author author)
|
|
{
|
|
_authorCollection.ReplaceOne(r => r.Id == author.Id, author);
|
|
}
|
|
|
|
public override void DeleteAuthor(int id)
|
|
{
|
|
_authorCollection.DeleteOne(author => author.Id == id);
|
|
}
|
|
public override Author GetAuthor(string name)
|
|
{
|
|
return _authorCollection.Find(author => author.Name == name).FirstOrDefault();
|
|
}
|
|
public override void DeleteAllAuthors()
|
|
{
|
|
_authorCollection.DeleteManyAsync(Builders<Author>.Filter.Empty);
|
|
}
|
|
|
|
// Photo
|
|
public override void CreatePhoto(Photo photo)
|
|
{
|
|
if (photo.Id == 0)
|
|
{
|
|
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Photo").FirstOrDefault();
|
|
sequence.Count++;
|
|
photo.Id = sequence.Count;
|
|
_sequenceCollection.ReplaceOne(seq => seq.Id == "Photo", sequence);
|
|
}
|
|
_photoCollection.InsertOne(photo);
|
|
}
|
|
|
|
public override List<Photo> GetPhotos()
|
|
{
|
|
return _photoCollection.Find(_ => true).ToList();
|
|
}
|
|
|
|
public override Photo GetPhoto(int id)
|
|
{
|
|
return _photoCollection.Find(photo => photo.Id == id).FirstOrDefault();
|
|
}
|
|
|
|
public override void UpdatePhoto(Photo photo)
|
|
{
|
|
_photoCollection.ReplaceOne(b => b.Id == photo.Id, photo);
|
|
}
|
|
|
|
public override void DeletePhoto(int id)
|
|
{
|
|
_photoCollection.DeleteOne(photo => photo.Id == id);
|
|
}
|
|
public override Photo GetPhoto(int AlbumId, int LocationId, int AuthorId)
|
|
{
|
|
return _photoCollection.Find(photo => (photo.AlbumId == AlbumId && photo.LocationId == LocationId && photo.AuthorId == AuthorId)).FirstOrDefault();
|
|
}
|
|
public override void DeleteAllPhotos()
|
|
{
|
|
_photoCollection.DeleteManyAsync(Builders<Photo>.Filter.Empty);
|
|
}
|
|
|
|
// Comment
|
|
public override void CreateComment(Comment comment)
|
|
{
|
|
if (comment.Id == 0)
|
|
{
|
|
Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "Comment").FirstOrDefault();
|
|
sequence.Count++;
|
|
comment.Id = sequence.Count;
|
|
_sequenceCollection.ReplaceOne(seq => seq.Id == "Comment", sequence);
|
|
}
|
|
_commentCollection.InsertOne(comment);
|
|
}
|
|
|
|
public override List<Comment> GetComments()
|
|
{
|
|
return _commentCollection.Find(_ => true).ToList();
|
|
}
|
|
|
|
public override Comment GetComment(int id)
|
|
{
|
|
return _commentCollection.Find(comment => comment.Id == id).FirstOrDefault();
|
|
}
|
|
|
|
public override void UpdateComment(Comment comment)
|
|
{
|
|
_commentCollection.ReplaceOne(b => b.Id == comment.Id, comment);
|
|
}
|
|
|
|
public override void DeleteComment(int id)
|
|
{
|
|
_commentCollection.DeleteOne(comment => comment.Id == id);
|
|
}
|
|
public override Comment GetCommentPhoto(int PhotoId)
|
|
{
|
|
return _commentCollection.Find(comment => comment.PhotoId == PhotoId).FirstOrDefault();
|
|
}
|
|
public override void DeleteAllComments()
|
|
{
|
|
_commentCollection.DeleteManyAsync(Builders<Comment>.Filter.Empty);
|
|
}
|
|
|
|
public void UpdateSequence(Sequence sequence)
|
|
{
|
|
_sequenceCollection.ReplaceOne(seq => seq.Id == sequence.Id, sequence);
|
|
}
|
|
}
|
|
}
|