-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from scogliera/feature/add_dal
Propojení projektu s MongoDB
- Loading branch information
Showing
7 changed files
with
91 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using TeacherDigitalAgency.Models; | ||
|
||
namespace TeacherDigitalAgency.DAL; | ||
|
||
public interface IMongoDal | ||
{ | ||
public Lecturer? GetLecturer(Guid id); | ||
public IEnumerable<Lecturer> GetAllLecturers(); | ||
public void SetLecturer(Lecturer lecturer); | ||
public bool DeleteLecturer(Guid id); | ||
public void AddLecturer(Lecturer lecturer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using MongoDB.Driver; | ||
using TeacherDigitalAgency.Models; | ||
|
||
namespace TeacherDigitalAgency.DAL; | ||
|
||
public class MongoDal: IMongoDal | ||
{ | ||
private readonly IMongoCollection<Lecturer> _lecturersCollection; | ||
|
||
public MongoDal(IConfiguration configuration) | ||
{ | ||
if (configuration == null) | ||
throw new ArgumentNullException(nameof(configuration)); | ||
|
||
var connectionString = configuration.GetConnectionString("MongoDb") ?? throw new ArgumentNullException(nameof(configuration)); | ||
|
||
var settings = MongoClientSettings.FromConnectionString(connectionString); | ||
settings.ServerApi = new ServerApi(ServerApiVersion.V1); | ||
|
||
var client = new MongoClient(settings); | ||
_lecturersCollection = client.GetDatabase("tda-db").GetCollection<Lecturer>("lecturers"); | ||
} | ||
|
||
public Lecturer? GetLecturer(Guid id) | ||
{ | ||
return _lecturersCollection.Find(lecturer => lecturer.Uuid == id).FirstOrDefault(); | ||
} | ||
|
||
public IEnumerable<Lecturer> GetAllLecturers() | ||
{ | ||
return _lecturersCollection.Find(_ => true).ToEnumerable(); | ||
} | ||
|
||
public void SetLecturer(Lecturer lecturer) | ||
{ | ||
var filter = Builders<Lecturer>.Filter.Eq(l => l.Uuid, lecturer.Uuid); | ||
var update = Builders<Lecturer>.Update | ||
.Set(l => l.TitleBefore, lecturer.TitleBefore) | ||
.Set(l => l.FirstName, lecturer.FirstName) | ||
.Set(l => l.MiddleName, lecturer.MiddleName) | ||
.Set(l => l.LastName, lecturer.LastName) | ||
.Set(l => l.TitleAfter, lecturer.TitleAfter) | ||
.Set(l => l.PictureUrl, lecturer.PictureUrl) | ||
.Set(l => l.Location, lecturer.Location) | ||
.Set(l => l.Claim, lecturer.Claim) | ||
.Set(l => l.Bio, lecturer.Bio) | ||
.Set(l => l.Tags, lecturer.Tags) | ||
.Set(l => l.PricePerHour, lecturer.PricePerHour) | ||
.Set(l => l.ContactInfo, lecturer.ContactInfo); | ||
|
||
_lecturersCollection.UpdateOne(filter, update); | ||
} | ||
|
||
public bool DeleteLecturer(Guid id) | ||
{ | ||
var deleteResult = _lecturersCollection.DeleteOne(lecturer => lecturer.Uuid == id); | ||
return deleteResult.DeletedCount > 0 && deleteResult.IsAcknowledged; | ||
} | ||
|
||
public void AddLecturer(Lecturer lecturer) | ||
{ | ||
_lecturersCollection.InsertOne(lecturer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters