change on mongodb
This commit is contained in:
parent
8d2fc1a9d6
commit
53204eab06
@ -157,7 +157,11 @@ namespace Announcements
|
||||
comboBox1.Visible = false;
|
||||
button3.Visible = false;
|
||||
button1.Visible = true;
|
||||
label7.Text = "Дата обновления: " + (Id.changeDate < Id.createDate ? Id.createDate.Date : Id.changeDate.Date);
|
||||
if(Id.createDate.HasValue)
|
||||
{
|
||||
label7.Text = "Дата обновления: " + (Id.changeDate.Value < Id.createDate.Value ? (DateTime)Id.createDate.Value.Date : (DateTime)Id.changeDate.Value.Date);
|
||||
|
||||
}
|
||||
if (Id.isClose)
|
||||
{
|
||||
label3.Visible = true;
|
||||
|
@ -10,6 +10,8 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
|
||||
<PackageReference Include="MongoDB.Bson" Version="2.19.1" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.19.1" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
|
@ -1,4 +1,5 @@
|
||||
using Announcements.Models;
|
||||
using MongoDB.Bson;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
@ -1,25 +0,0 @@
|
||||
using Announcements.Database.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.VisualBasic.ApplicationServices;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Announcements.Database
|
||||
{
|
||||
public class DataBaseContext : DbContext
|
||||
{
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (optionsBuilder.IsConfigured == false)
|
||||
{
|
||||
optionsBuilder.UseNpgsql("Host=192.168.1.70;Port=5432;Database=database;Username=postgres;Password=123");
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -2,6 +2,8 @@
|
||||
using Announcements.Database.Models;
|
||||
using Announcements.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
@ -26,150 +28,235 @@ namespace Announcements.Database.Implements
|
||||
}
|
||||
public bool BlockingAnnouncement(IAnnouncement model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("announcement");
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw(@$"UPDATE public.blocking "+
|
||||
@$"SET blockingid = {model.blockingId} WHERE id = {model.id}", new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var filter = Builders<BsonDocument>.Filter.Eq("_id", model.id);
|
||||
|
||||
var update = Builders<BsonDocument>.Update.Set("BlockingId", model.blockingId);
|
||||
|
||||
collection.UpdateOne(filter, update);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool CreateAnnouncement(IAnnouncement model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("announcement");
|
||||
|
||||
var reviews = new BsonArray();
|
||||
|
||||
//model.reviews.ToList().Select(review => reviews.Add(new BsonDocument
|
||||
//{
|
||||
// ["_id"] = review.id,
|
||||
// ["Text"] = review.text,
|
||||
// ["Complaints"] = review.complaints,
|
||||
// ["AuthorID"] = review.authorId.ToString(),
|
||||
// ["BlockingId"] = review.blockingId.ToString()
|
||||
//}));
|
||||
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
var update = new BsonDocument
|
||||
{
|
||||
var script = @$"INSERT INTO public.announcement " +
|
||||
@$"(text, createdate, changedate, complaints, isclose, categoryid, authorid, blockingid)" +
|
||||
$@"VALUES ('{model.text}', '{model.createDate}', '{model.changeDate}', " +
|
||||
$@"{model.complaints}, {model.isClose}, {model.categoryId.id}, {model.authorId.id}, {model.blockingId?.id.ToString()}); ";
|
||||
_db.Database.ExecuteSqlRaw(script, new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
{"_id", model.id+1},
|
||||
{ "Text", model.text},
|
||||
|
||||
{"Createdate", model.createDate},
|
||||
|
||||
{"Changedate", model.changeDate},
|
||||
|
||||
{"Complaints", model.complaints},
|
||||
|
||||
{"Isclose", model.isClose},
|
||||
|
||||
{"Review", reviews},
|
||||
|
||||
{"CategoryId", model.categoryId.id.ToString()},
|
||||
|
||||
{"AuthorId", model.authorId.id.ToString()}
|
||||
//{ "BlockingId", model.blockingId?.ToString()}
|
||||
};
|
||||
collection.InsertOne(update);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool DeleteAnnouncement(IAnnouncement model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("announcement");
|
||||
|
||||
var filter = Builders<BsonDocument>.Filter.Eq("_id", model.id);
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw(@$"DELETE FROM public.announcement " +
|
||||
@$" WHERE id = {model.id}; ", new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
collection.DeleteOne(filter);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public IAnnouncement? ReadElement(int id)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("announcement");
|
||||
var filter = Builders<BsonDocument>.Filter.Eq("_id", id);
|
||||
|
||||
var findAnn = collection.Find(filter).FirstOrDefault();
|
||||
|
||||
var reviews = new BsonArray();
|
||||
|
||||
//var rev = new List.reviews.ToList().Select(review => reviews.Add(new BsonDocument
|
||||
//{
|
||||
// review.id = ["_id"],
|
||||
// ["Text"] = review.text,
|
||||
// ["Complaints"] = review.complaints,
|
||||
// ["AuthorID"] = review.authorId.ToString(),
|
||||
// ["BlockingId"] = review.blockingId.ToString()
|
||||
//}));
|
||||
|
||||
IAnnouncement announcement = new Announcement
|
||||
{
|
||||
using (var command = _db.Database.GetDbConnection().CreateCommand())
|
||||
{
|
||||
command.CommandText = $"SELECT * FROM public.announcement WHERE id = {id}";
|
||||
command.CommandType = CommandType.Text;
|
||||
id = Convert.ToInt32(findAnn["_id"]),
|
||||
text = findAnn["Text"].ToString(),
|
||||
|
||||
_db.Database.OpenConnection();
|
||||
createDate = findAnn["Createdate"].ToString().ToLower() == "bsonnull" ? null : Convert.ToDateTime(findAnn["Createdate"]),
|
||||
|
||||
using (var result = command.ExecuteReader())
|
||||
{
|
||||
Announcement announcement = new Announcement();
|
||||
changeDate = findAnn["Changedate"].ToString().ToLower() == "bsonnull" ? null : Convert.ToDateTime(findAnn["Changedate"]),
|
||||
|
||||
while (result.Read())
|
||||
{
|
||||
announcement = new Announcement
|
||||
{
|
||||
id = Convert.ToInt32(result["id"]),
|
||||
text = result["text"].ToString(),
|
||||
createDate = Convert.ToDateTime(result["createdate"]),
|
||||
changeDate = Convert.ToDateTime(result["changedate"]),
|
||||
complaints = Convert.ToInt32(result["complaints"]),
|
||||
isClose = (bool)result["isclose"],
|
||||
categoryId = categoryLogic.ReadElement(Convert.ToInt32(result["categoryId"])),
|
||||
authorId = userLogic.ReadElement(Convert.ToInt32(result["authorid"])),
|
||||
blockingId = blockingLogic.ReadElement(Convert.ToInt32(result["blockingid"]))
|
||||
};
|
||||
}
|
||||
complaints = Convert.ToInt32(findAnn["Complaints"]),
|
||||
|
||||
return announcement;
|
||||
}
|
||||
}
|
||||
}
|
||||
isClose = Convert.ToBoolean(findAnn["Isclose"]),
|
||||
|
||||
reviews = new IReview [5],
|
||||
|
||||
categoryId = categoryLogic.ReadElement(Convert.ToInt32(findAnn["CategoryId"])),
|
||||
|
||||
authorId = userLogic.ReadElement(Convert.ToInt32(findAnn["AuthorId"])),
|
||||
|
||||
blockingId = findAnn["BlockingId"].ToString().ToLower() != "bsonnull" ? blockingLogic.ReadElement(Convert.ToInt32(findAnn["BlockingId"])) : null
|
||||
};
|
||||
return announcement;
|
||||
}
|
||||
|
||||
public List<IAnnouncement>? ReadList()
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("announcement");
|
||||
|
||||
var entities = new List<IAnnouncement>();
|
||||
|
||||
var ann = collection.Find(new BsonDocument()).ToList();
|
||||
var u = ann[0];
|
||||
ann.ForEach(u =>
|
||||
{
|
||||
using (var command = _db.Database.GetDbConnection().CreateCommand())
|
||||
try
|
||||
{
|
||||
command.CommandText = "SELECT * FROM public.announcement";
|
||||
command.CommandType = CommandType.Text;
|
||||
|
||||
_db.Database.OpenConnection();
|
||||
|
||||
using (var result = command.ExecuteReader())
|
||||
entities.Add(new Announcement
|
||||
{
|
||||
var entities = new List<IAnnouncement>();
|
||||
id = Convert.ToInt32(u["_id"]),
|
||||
text = u["Text"].ToString(),
|
||||
|
||||
while (result.Read())
|
||||
{
|
||||
entities.Add(new Announcement
|
||||
{
|
||||
id = Convert.ToInt32(result["id"]),
|
||||
text = result["text"].ToString(),
|
||||
createDate = Convert.ToDateTime(result["createdate"]),
|
||||
changeDate = Convert.ToDateTime(result["changedate"]),
|
||||
complaints = Convert.ToInt32(result["complaints"]),
|
||||
isClose = (bool)result["isclose"],
|
||||
categoryId = categoryLogic.ReadElement(Convert.ToInt32(result["categoryid"])),
|
||||
authorId = userLogic.ReadElement(Convert.ToInt32(result["authorid"])),
|
||||
blockingId = blockingLogic.ReadElement(Convert.ToInt32(result["blockingid"]))
|
||||
});
|
||||
}
|
||||
//createDate = u["Createdate"].ToString().ToLower() == "bsonnull" ? null : Convert.ToDateTime(u["Createdate"]),
|
||||
|
||||
return entities;
|
||||
}
|
||||
//changeDate = u["Changedate"].ToString().ToLower() == "bsonnull" ? null : Convert.ToDateTime(u["Changedate"]),
|
||||
|
||||
complaints = Convert.ToInt32(u["Complaints"]),
|
||||
|
||||
isClose = Convert.ToBoolean(u["Isclose"]),
|
||||
|
||||
reviews = new Review[5],
|
||||
|
||||
categoryId = (Category)categoryLogic.ReadElement(Convert.ToInt32(u["CategoryId"])),
|
||||
|
||||
authorId = (User)userLogic.ReadElement(Convert.ToInt32(u["AuthorId"])),
|
||||
|
||||
blockingId = (Blocking?)(u["BlockingId"].ToString().ToLower() == "bsonnull" ? null : blockingLogic.ReadElement(Convert.ToInt32(u["BlockingId"])))
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { }
|
||||
});
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
|
||||
public bool UpdateAnnouncement(IAnnouncement model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("announcement");
|
||||
|
||||
var reviews = new BsonArray();
|
||||
|
||||
if(reviews != null && reviews.Count > 0)
|
||||
{
|
||||
try
|
||||
model.reviews.ToList().Select(review => reviews.Add(new BsonDocument
|
||||
{
|
||||
var script = @$"UPDATE public.announcement " +
|
||||
$"SET text='{model.text}', createdate='{model.createDate}', changedate='{model.changeDate}', complaints={model.complaints}, " +
|
||||
$"isclose={model.isClose}, categoryid={model.categoryId.id}, authorid={model.authorId.id}, blockingid={model.blockingId.id} " +
|
||||
$"WHERE id = {model.id}";
|
||||
_db.Database.ExecuteSqlRaw(script, new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
["_id"] = review.id,
|
||||
["Text"] = review.text,
|
||||
["Complaints"] = review.complaints,
|
||||
["AuthorID"] = review.authorId.ToString(),
|
||||
["BlockingId"] = review.blockingId.ToString()
|
||||
}));
|
||||
}
|
||||
|
||||
var filter = Builders<BsonDocument>.Filter.Eq("_id", model.id);
|
||||
|
||||
try
|
||||
{
|
||||
var update = new BsonDocument
|
||||
{
|
||||
return false;
|
||||
}
|
||||
{"_id", model.id},
|
||||
{ "Text", model.text},
|
||||
|
||||
{"Createdate", model.createDate},
|
||||
|
||||
{"Changedate", model.changeDate},
|
||||
|
||||
{"Complaints", model.complaints},
|
||||
|
||||
{"Isclose", model.isClose},
|
||||
|
||||
{"Review", reviews},
|
||||
|
||||
{"CategoryId", model.categoryId.id.ToString()},
|
||||
|
||||
{"AuthorId", model.authorId.id.ToString()},
|
||||
{ "BlockingId", model.blockingId.id.ToString()}
|
||||
};
|
||||
collection.UpdateOne(filter, update);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@
|
||||
using Announcements.Database.Models;
|
||||
using Announcements.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
@ -15,115 +17,114 @@ namespace Announcements.Database.Implements
|
||||
{
|
||||
public bool CreateBlocking(IBlocking model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("blocking");
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
var update = new BsonDocument
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw(@$"INSERT INTO public.blocking" +
|
||||
@$"(name)" +
|
||||
@$"VALUES ('{model.name}');", new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
{"_id", model.id},
|
||||
{ "Name", model.name}
|
||||
};
|
||||
collection.InsertOne(update);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool DeleteBlocking(IBlocking model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("blocking");
|
||||
|
||||
var filter = Builders<BsonDocument>.Filter.Eq("_id", model.id);
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw(@$"DELETE FROM public.blocking " +
|
||||
@$" WHERE id = {model.id}; ", new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
collection.DeleteOne(filter);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public IBlocking? ReadElement(int id)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("blocking");
|
||||
|
||||
var filter = Builders<BsonDocument>.Filter.Eq("_id", id);
|
||||
|
||||
var findBlock = collection.Find(filter).FirstOrDefault();
|
||||
if(findBlock != null)
|
||||
{
|
||||
using (var command = _db.Database.GetDbConnection().CreateCommand())
|
||||
IBlocking blocking = new Blocking
|
||||
{
|
||||
command.CommandText = $"SELECT * FROM public.blocking WHERE id = {id}";
|
||||
command.CommandType = CommandType.Text;
|
||||
id = Convert.ToInt32(findBlock["_id"]),
|
||||
name = findBlock["Name"].ToString()
|
||||
};
|
||||
|
||||
_db.Database.OpenConnection();
|
||||
|
||||
using (var result = command.ExecuteReader())
|
||||
{
|
||||
Blocking blocking = new Blocking();
|
||||
|
||||
while (result.Read())
|
||||
{
|
||||
blocking = new Blocking
|
||||
{
|
||||
id = Convert.ToInt32(result["id"]),
|
||||
name = result["name"].ToString()
|
||||
};
|
||||
}
|
||||
|
||||
return blocking;
|
||||
}
|
||||
}
|
||||
return blocking;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<IBlocking>? ReadList()
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("blocking");
|
||||
|
||||
var entities = new List<IBlocking>();
|
||||
|
||||
var blockings = collection.Find(new BsonDocument()).ToList();
|
||||
|
||||
blockings.ForEach(u =>
|
||||
{
|
||||
using (var command = _db.Database.GetDbConnection().CreateCommand())
|
||||
entities.Add(new Blocking
|
||||
{
|
||||
command.CommandText = "SELECT * FROM public.blocking";
|
||||
command.CommandType = CommandType.Text;
|
||||
id = Convert.ToInt32(u["_id"]),
|
||||
name = u["Name"].ToString()
|
||||
});
|
||||
});
|
||||
|
||||
_db.Database.OpenConnection();
|
||||
|
||||
using (var result = command.ExecuteReader())
|
||||
{
|
||||
var entities = new List<IBlocking>();
|
||||
|
||||
while (result.Read())
|
||||
{
|
||||
entities.Add(new Blocking
|
||||
{
|
||||
id = Convert.ToInt32(result["id"]),
|
||||
name = result["name"].ToString()
|
||||
});
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
}
|
||||
}
|
||||
return entities;
|
||||
}
|
||||
|
||||
|
||||
public bool UpdateBlocking(IBlocking model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("blocking");
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
var filter = Builders<BsonDocument>.Filter.Eq("_id", model.id);
|
||||
var update = new BsonDocument
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw(@$"UPDATE public.blocking " +
|
||||
@$"SET name = {model.name} " +
|
||||
@$"WHERE id = {model.id}", new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
{"_id", model.id},
|
||||
{ "Name", model.name}
|
||||
};
|
||||
collection.UpdateOne(filter, update);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@
|
||||
using Announcements.Database.Models;
|
||||
using Announcements.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
@ -15,114 +17,114 @@ namespace Announcements.Database.Implements
|
||||
{
|
||||
public bool CreateCategory(ICategory model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("category");
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
var update = new BsonDocument
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw(@$"INSERT INTO public.category" +
|
||||
@$"(name)" +
|
||||
@$"VALUES ('{model.name}');", new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
{"_id", model.id},
|
||||
{ "Name", model.name}
|
||||
};
|
||||
collection.InsertOne(update);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool DeleteCategory(ICategory model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("category");
|
||||
|
||||
var filter = Builders<BsonDocument>.Filter.Eq("_id", model.id);
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw(@$"DELETE FROM public.category " +
|
||||
@$" WHERE id = {model.id}; ", new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
collection.DeleteOne(filter);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public ICategory? ReadElement(int id)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("category");
|
||||
|
||||
var filter = Builders<BsonDocument>.Filter.Eq("_id", id);
|
||||
|
||||
var findUser = collection.Find(filter).FirstOrDefault();
|
||||
|
||||
ICategory category = new Category
|
||||
{
|
||||
using (var command = _db.Database.GetDbConnection().CreateCommand())
|
||||
{
|
||||
command.CommandText = $"SELECT * FROM public.category WHERE id = {id}";
|
||||
command.CommandType = CommandType.Text;
|
||||
id = Convert.ToInt32(findUser["_id"]),
|
||||
name = findUser["Name"].ToString()
|
||||
};
|
||||
|
||||
_db.Database.OpenConnection();
|
||||
|
||||
using (var result = command.ExecuteReader())
|
||||
{
|
||||
Category category = new Category();
|
||||
|
||||
while (result.Read())
|
||||
{
|
||||
category = new Category
|
||||
{
|
||||
id = Convert.ToInt32(result["id"]),
|
||||
name = result["name"].ToString()
|
||||
};
|
||||
}
|
||||
|
||||
return category;
|
||||
}
|
||||
}
|
||||
}
|
||||
return category;
|
||||
}
|
||||
|
||||
public List<ICategory>? ReadList()
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("category");
|
||||
|
||||
var entities = new List<ICategory>();
|
||||
|
||||
var categories = collection.Find(new BsonDocument()).ToList();
|
||||
|
||||
categories.ForEach(u =>
|
||||
{
|
||||
using (var command = _db.Database.GetDbConnection().CreateCommand())
|
||||
if(!u.Contains("_Id"))
|
||||
{
|
||||
command.CommandText = "SELECT * FROM public.category";
|
||||
command.CommandType = CommandType.Text;
|
||||
|
||||
_db.Database.OpenConnection();
|
||||
|
||||
using (var result = command.ExecuteReader())
|
||||
entities.Add(new Category
|
||||
{
|
||||
var entities = new List<ICategory>();
|
||||
|
||||
while (result.Read())
|
||||
{
|
||||
entities.Add(new Category
|
||||
{
|
||||
id = Convert.ToInt32(result["id"]),
|
||||
name = result["name"].ToString()
|
||||
});
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
id = Convert.ToInt32(u["_id"]),
|
||||
name = u["Name"].ToString()
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
public bool UpdateCategory(ICategory model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("category");
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
var filter = Builders<BsonDocument>.Filter.Eq("_id", model.id);
|
||||
var update = new BsonDocument
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw(@$"UPDATE public.category " +
|
||||
@$"SET name = {model.name} "+
|
||||
@$"WHERE id = {model.id}", new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
{"_id", model.id},
|
||||
{ "Name", model.name}
|
||||
};
|
||||
collection.UpdateOne(filter, update);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@
|
||||
using Announcements.Database.Models;
|
||||
using Announcements.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
@ -26,143 +28,130 @@ namespace Announcements.Database.Implements
|
||||
|
||||
public bool BlockingReview(IReview model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("review");
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw(@$"UPDATE public.review SET isadmin = true WHERE id = {model.id}", new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var filter = Builders<BsonDocument>.Filter.Eq("_id", model.id);
|
||||
|
||||
var update = Builders<BsonDocument>.Update.Set("BlockingId", model.blockingId);
|
||||
|
||||
collection.UpdateOne(filter, update);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool CreateReview(IReview model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
{
|
||||
try
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw(@$"INSERT INTO public.review "+
|
||||
$"(text, createdate, complaints, announcementid, authorid, blockingid) "+
|
||||
$"VALUES('{model.text}', '{model.createDate}', {model.complaints}, {model.announcementId.id}, "+
|
||||
$"{model.authorId.id}, {model.blockingId.id}); ", new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
//var database = dbClient.GetDatabase("database");
|
||||
//var collection = database.GetCollection<BsonDocument>("announcement");
|
||||
//var filter = Builders<BsonDocument>.Filter.Eq("_id", model.id);
|
||||
|
||||
//var update = new
|
||||
|
||||
//model.reviews.ToList().Select(review => reviews.Add(new BsonDocument
|
||||
//{
|
||||
// ["_id"] = review.id,
|
||||
// ["Text"] = review.text,
|
||||
// ["Complaints"] = review.complaints,
|
||||
// ["AuthorID"] = review.authorId.ToString(),
|
||||
// ["BlockingId"] = review.blockingId.ToString()
|
||||
//}));
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool DeleteReview(IReview model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
{
|
||||
try
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw(@$"DELETE FROM public.review " +
|
||||
@$" WHERE id = {model.id}; ", new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//using (var _db = new DataBaseContext())
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// _db.Database.ExecuteSqlRaw(@$"DELETE FROM public.review " +
|
||||
// @$" WHERE id = {model.id}; ", new object[] { });
|
||||
// return true;
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
//}
|
||||
return true;
|
||||
}
|
||||
|
||||
public IReview? ReadElement(int id)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
{
|
||||
using (var command = _db.Database.GetDbConnection().CreateCommand())
|
||||
{
|
||||
command.CommandText = $"SELECT * FROM public.review WHERE id = {id}";
|
||||
command.CommandType = CommandType.Text;
|
||||
//MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
_db.Database.OpenConnection();
|
||||
|
||||
using (var result = command.ExecuteReader())
|
||||
{
|
||||
Review review = new Review();
|
||||
|
||||
while (result.Read())
|
||||
{
|
||||
review = new Review
|
||||
{
|
||||
id = Convert.ToInt32(result["id"]),
|
||||
text = result["text"].ToString(),
|
||||
createDate = Convert.ToDateTime(result["createdate"]),
|
||||
complaints = Convert.ToInt32(result["complaints"]),
|
||||
announcementId = announcementLogic.ReadElement(Convert.ToInt32(result["announcementId"])),
|
||||
authorId = userLogic.ReadElement(Convert.ToInt32(result["authorid"])),
|
||||
blockingId = blockingLogic.ReadElement(Convert.ToInt32(result["blockingId"]))
|
||||
};
|
||||
}
|
||||
|
||||
return review;
|
||||
}
|
||||
}
|
||||
}
|
||||
//var database = dbClient.GetDatabase("database");
|
||||
//var collection = database.GetCollection<BsonDocument>("announcement");
|
||||
//var filter = Builders<BsonDocument>.Filter.Eq("student_id", 10000);
|
||||
return new Review();
|
||||
|
||||
}
|
||||
|
||||
public List<IReview>? ReadList()
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
{
|
||||
using (var command = _db.Database.GetDbConnection().CreateCommand())
|
||||
{
|
||||
command.CommandText = $"SELECT * FROM public.review";
|
||||
command.CommandType = CommandType.Text;
|
||||
//using (var _db = new DataBaseContext())
|
||||
//{
|
||||
// using (var command = _db.Database.GetDbConnection().CreateCommand())
|
||||
// {
|
||||
// command.CommandText = $"SELECT * FROM public.review";
|
||||
// command.CommandType = CommandType.Text;
|
||||
|
||||
_db.Database.OpenConnection();
|
||||
// _db.Database.OpenConnection();
|
||||
|
||||
using (var result = command.ExecuteReader())
|
||||
{
|
||||
var reviews = new List<IReview>();
|
||||
// using (var result = command.ExecuteReader())
|
||||
// {
|
||||
// var reviews = new List<IReview>();
|
||||
|
||||
while (result.Read())
|
||||
{
|
||||
reviews.Add(new Review
|
||||
{
|
||||
id = Convert.ToInt32(result["id"]),
|
||||
text = result["text"].ToString(),
|
||||
createDate = Convert.ToDateTime(result["createdate"]),
|
||||
complaints = Convert.ToInt32(result["complaints"]),
|
||||
announcementId = announcementLogic.ReadElement(Convert.ToInt32(result["announcementId"])),
|
||||
authorId = userLogic.ReadElement(Convert.ToInt32(result["authorid"])),
|
||||
blockingId = blockingLogic.ReadElement(Convert.ToInt32(result["blockingId"]))
|
||||
});
|
||||
}
|
||||
// while (result.Read())
|
||||
// {
|
||||
// reviews.Add(new Review
|
||||
// {
|
||||
// id = Convert.ToInt32(result["id"]),
|
||||
// text = result["text"].ToString(),
|
||||
// createDate = Convert.ToDateTime(result["createdate"]),
|
||||
// complaints = Convert.ToInt32(result["complaints"]),
|
||||
// announcementId = announcementLogic.ReadElement(Convert.ToInt32(result["announcementId"])),
|
||||
// authorId = userLogic.ReadElement(Convert.ToInt32(result["authorid"])),
|
||||
// blockingId = blockingLogic.ReadElement(Convert.ToInt32(result["blockingId"]))
|
||||
// });
|
||||
// }
|
||||
|
||||
return reviews;
|
||||
}
|
||||
}
|
||||
}
|
||||
// return reviews;
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
return new List<IReview>();
|
||||
}
|
||||
|
||||
public bool UpdateReview(IReview model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
{
|
||||
try
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw(@$"UPDATE public.review "+
|
||||
@$"SET text={model.text}, createdate={model.createDate}, complaints={model.complaints}, announcementid={model.announcementId.id}, "+
|
||||
@$"authorid={model.authorId.id}, blockingid={model.blockingId.id} "+
|
||||
@$" WHERE id = ", new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//using (var _db = new DataBaseContext())
|
||||
//{
|
||||
// try
|
||||
// {
|
||||
// _db.Database.ExecuteSqlRaw(@$"UPDATE public.review "+
|
||||
// @$"SET text={model.text}, createdate={model.createDate}, complaints={model.complaints}, announcementid={model.announcementId.id}, "+
|
||||
// @$"authorid={model.authorId.id}, blockingid={model.blockingId.id} "+
|
||||
// @$" WHERE id = ", new object[] { });
|
||||
// return true;
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
//}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,8 @@
|
||||
using Announcements.Database.Models;
|
||||
using Announcements.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Driver;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
@ -15,146 +17,178 @@ namespace Announcements.Database.Implements
|
||||
{
|
||||
public bool BlockingUser(IUser model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("user");
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw(@$"UPDATE public.user SET isadmin = true WHERE id = {model.id}", new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
var filter = Builders<BsonDocument>.Filter.Eq("_id", model.id);
|
||||
|
||||
var update = Builders<BsonDocument>.Update.Set("isAdmin", true);
|
||||
|
||||
collection.UpdateOne(filter, update);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool CreateUser(IUser model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("user");
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
var update = new BsonDocument
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw(@$"INSERT INTO public.user"+
|
||||
@$"(name, surname, patronymic, email, password, registrdate, rating, isadmin)"+
|
||||
@$"VALUES ('{model.name}', '{model.surname}', '{model.patronymic}', '{model.email}', "+
|
||||
@$"'{model.password}', '{model.registrDate}'::timestamp, '{model.rating}', '{model.isAdmin}');", new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
{"_id", model.id},
|
||||
{ "Name", model.name},
|
||||
|
||||
{"Surname", model.surname},
|
||||
|
||||
{"Patronymic", model.patronymic},
|
||||
|
||||
{"Email", model.email},
|
||||
|
||||
{"Password", model.password},
|
||||
|
||||
{"Registerdate", model.registrDate},
|
||||
|
||||
{"Rating", model.rating},
|
||||
|
||||
{"isAdmin", model.isAdmin }
|
||||
};
|
||||
collection.InsertOne(update);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public bool DeleteUser(IUser model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("user");
|
||||
|
||||
var filter = Builders<BsonDocument>.Filter.Eq("_id", model.id);
|
||||
try
|
||||
{
|
||||
try
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw(@$"DELETE FROM public.user "+
|
||||
@$" WHERE id = {model.id}; ", new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
collection.DeleteOne(filter);
|
||||
return true;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public IUser? ReadElement(int id)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
{
|
||||
using (var command = _db.Database.GetDbConnection().CreateCommand())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("user");
|
||||
|
||||
var filter = Builders<BsonDocument>.Filter.Eq("_id", id);
|
||||
|
||||
var findUser = collection.Find(filter).FirstOrDefault();
|
||||
|
||||
IUser user = new User
|
||||
{
|
||||
command.CommandText = $"SELECT * FROM public.user WHERE id = {id}";
|
||||
command.CommandType = CommandType.Text;
|
||||
id = Convert.ToInt32(findUser["_id"]),
|
||||
name = findUser["Name"].ToString(),
|
||||
surname = findUser["Surname"].ToString(),
|
||||
patronymic = findUser["Patronymic"].ToString(),
|
||||
password = findUser["Password"].ToString(),
|
||||
email = findUser["Email"].ToString(),
|
||||
registrDate = findUser["Registerdate"].ToString().ToLower() == "bsonnull" ? null : Convert.ToDateTime(findUser["Registerdate"]),
|
||||
//rating = (Convert.ToDouble(findUser["Rating"])),
|
||||
isAdmin = Convert.ToBoolean(findUser["isAdmin"])
|
||||
};
|
||||
|
||||
_db.Database.OpenConnection();
|
||||
|
||||
using (var result = command.ExecuteReader())
|
||||
{
|
||||
User user = new User();
|
||||
|
||||
while (result.Read())
|
||||
{
|
||||
user = new User
|
||||
{
|
||||
id = Convert.ToInt32(result["id"]),
|
||||
name = result["name"].ToString(),
|
||||
surname = result["surname"].ToString(),
|
||||
patronymic = result["patronymic"].ToString(),
|
||||
password = result["password"].ToString(),
|
||||
email = result["email"].ToString(),
|
||||
registrDate = Convert.ToDateTime(result["registrdate"]),
|
||||
rating = (float)Convert.ToDecimal(result["rating"]),
|
||||
isAdmin = (bool)result["isadmin"]
|
||||
};
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
}
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
public List<IUser>? ReadList()
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("user");
|
||||
|
||||
var entities = new List<IUser>();
|
||||
|
||||
var users = collection.Find(new BsonDocument()).ToList();
|
||||
|
||||
users.ForEach(u =>
|
||||
{
|
||||
using (var command = _db.Database.GetDbConnection().CreateCommand())
|
||||
DateTime res;
|
||||
DateTime.TryParse(u["Registerdate"].ToString(), out res);
|
||||
entities.Add(new User
|
||||
{
|
||||
command.CommandText = "SELECT * FROM public.user";
|
||||
command.CommandType = CommandType.Text;
|
||||
id = Convert.ToInt32(u["_id"]),
|
||||
name = u["Name"].ToString(),
|
||||
surname = u["Surname"].ToString(),
|
||||
patronymic = u["Patronymic"].ToString(),
|
||||
password = u["Password"].ToString(),
|
||||
email = u["Email"].ToString(),
|
||||
registrDate = res == null ? DateTime.UtcNow : res,
|
||||
rating = (float)(Convert.ToDouble(u["Rating"])),
|
||||
isAdmin = (bool)u["isAdmin"]
|
||||
});
|
||||
});
|
||||
|
||||
_db.Database.OpenConnection();
|
||||
|
||||
using (var result = command.ExecuteReader())
|
||||
{
|
||||
var entities = new List<IUser>();
|
||||
|
||||
while (result.Read())
|
||||
{
|
||||
entities.Add(new User
|
||||
{
|
||||
id = Convert.ToInt32(result["id"]),
|
||||
name = result["name"].ToString(),
|
||||
surname = result["surname"].ToString(),
|
||||
patronymic = result["patronymic"].ToString(),
|
||||
password = result["password"].ToString(),
|
||||
email = result["email"].ToString(),
|
||||
registrDate = Convert.ToDateTime(result["registrdate"]),
|
||||
rating = (float)(Convert.ToDouble(result["rating"])),
|
||||
isAdmin = (bool)result["isadmin"]
|
||||
});
|
||||
}
|
||||
|
||||
return entities;
|
||||
}
|
||||
}
|
||||
}
|
||||
return entities;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public bool UpdateUser(IUser model)
|
||||
{
|
||||
using (var _db = new DataBaseContext())
|
||||
MongoClient dbClient = new MongoClient(@"mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2");
|
||||
|
||||
var database = dbClient.GetDatabase("database");
|
||||
var collection = database.GetCollection<BsonDocument>("user");
|
||||
|
||||
try
|
||||
{
|
||||
try
|
||||
var filter = Builders<BsonDocument>.Filter.Eq("_id", model.id);
|
||||
var update = new BsonDocument
|
||||
{
|
||||
_db.Database.ExecuteSqlRaw(@$"UPDATE public.user "+
|
||||
@$"SET name = {model.name}, surname = {model.surname}, patronymic = {model.patronymic}, email = {model.email}, "+
|
||||
@$"password = {model.password}, registrdate = {model.registrDate}, rating = {model.rating}, isadmin = {model.isAdmin} "+
|
||||
@$"WHERE id = {model.id}", new object[] { });
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
{"_id", model.id},
|
||||
{ "Name", model.name},
|
||||
|
||||
{"Surname", model.surname},
|
||||
|
||||
{"Patronymic", model.patronymic},
|
||||
|
||||
{"Email", model.email},
|
||||
|
||||
{"Password", model.password},
|
||||
|
||||
{"Registerdate", model.registrDate},
|
||||
|
||||
{"Rating", model.rating},
|
||||
|
||||
{"isAdmin", model.isAdmin }
|
||||
};
|
||||
collection.UpdateOne(filter, update);
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -13,13 +13,14 @@ namespace Announcements.Database.Models
|
||||
|
||||
public string text { get; set; }
|
||||
|
||||
public DateTime createDate { get; set; }
|
||||
public DateTime? createDate { get; set; }
|
||||
|
||||
public DateTime changeDate { get; set; }
|
||||
public DateTime? changeDate { get; set; }
|
||||
|
||||
public int complaints { get; set; }
|
||||
|
||||
public bool isClose { get; set; }
|
||||
public IReview[] reviews { get; set; }
|
||||
|
||||
public ICategory categoryId { get; set; }
|
||||
|
||||
|
@ -21,7 +21,7 @@ namespace Announcements.Database.Models
|
||||
|
||||
public string password { get; set; }
|
||||
|
||||
public DateTime registrDate { get; set; }
|
||||
public DateTime? registrDate { get; set; }
|
||||
|
||||
public float rating { get; set; }
|
||||
|
||||
|
@ -10,10 +10,11 @@ namespace Announcements.Models
|
||||
{
|
||||
int id { get; }
|
||||
string text { get; }
|
||||
DateTime createDate { get; }
|
||||
DateTime changeDate { get; }
|
||||
DateTime? createDate { get; }
|
||||
DateTime? changeDate { get; }
|
||||
int complaints { get; }
|
||||
bool isClose { get; }
|
||||
IReview[] reviews { get; }
|
||||
ICategory categoryId { get; }
|
||||
IUser authorId { get; }
|
||||
IBlocking blockingId { get; }
|
||||
|
@ -14,7 +14,7 @@ namespace Announcements.Models
|
||||
string patronymic {get; }
|
||||
string email {get; }
|
||||
string password {get; }
|
||||
DateTime registrDate { get; }
|
||||
DateTime? registrDate { get; }
|
||||
float rating { get; }
|
||||
bool isAdmin {get; }
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user