Able to create and read data from the DB
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
using System.Linq;
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
using TextSender_API.Models;
|
||||
using TextSender_API.Repositories;
|
||||
|
||||
namespace TextSender_API.Controllers;
|
||||
|
||||
@@ -10,22 +13,46 @@ public class RegisterController : ControllerBase
|
||||
{
|
||||
#region Fields
|
||||
private readonly ILogger<RegisterController> _logger;
|
||||
private IConfiguration _config;
|
||||
#endregion
|
||||
|
||||
|
||||
#region Constructors
|
||||
|
||||
public RegisterController(ILogger<RegisterController> logger)
|
||||
public RegisterController(ILogger<RegisterController> logger, IConfiguration config)
|
||||
{
|
||||
this._logger = logger;
|
||||
this._config = config;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Methods
|
||||
[HttpPost("register"), DisableRequestSizeLimit]
|
||||
public IActionResult Upload([FromForm(Name = "file")] List<IFormFile> contactFile,
|
||||
[FromForm(Name = "user_id")] string userID)
|
||||
public IActionResult Upload([FromBody] User userRequest)
|
||||
{
|
||||
try
|
||||
{
|
||||
var connString = this._config.GetConnectionString("MongoDBURI");
|
||||
var userRepo = new UserRepository(connString);
|
||||
var allUsers = userRepo.RetrieveAllUsers();
|
||||
var result = allUsers.Exists(ea => ea.Username.Equals(userRequest.Username));
|
||||
|
||||
if (!result)
|
||||
{
|
||||
this._logger.LogInformation("Creating user");
|
||||
userRequest.DateCreated = DateTime.Now;
|
||||
userRepo.CreateUser(userRequest);
|
||||
}
|
||||
else
|
||||
{
|
||||
this._logger.LogInformation("User already exists");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
this._logger.LogError($"An error occurred: {ex.Message}");
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
#endregion
|
||||
|
||||
+4
-2
@@ -2,6 +2,7 @@ using System;
|
||||
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDB.Bson.Serialization.Conventions;
|
||||
using MongoDB.Driver;
|
||||
|
||||
namespace TextSender_API.Models;
|
||||
@@ -9,8 +10,9 @@ namespace TextSender_API.Models;
|
||||
public class User
|
||||
{
|
||||
#region Properties
|
||||
// [BsonElement("_id")]
|
||||
public ObjectId Id { get; set; }
|
||||
[BsonId]
|
||||
[BsonRepresentation(BsonType.ObjectId)]
|
||||
public string? Id { get; set; }
|
||||
[BsonElement("firstname")]
|
||||
public string? Firstname { get; set; }
|
||||
[BsonElement("lastname")]
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddControllers().AddNewtonsoftJson();
|
||||
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
@@ -16,6 +20,7 @@ if (app.Environment.IsDevelopment())
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
@@ -14,6 +14,7 @@ public class UserRepository
|
||||
private string _connectionString;
|
||||
private string _databaseName;
|
||||
private string _tableName;
|
||||
private readonly IMongoCollection<User> _bookCollection;
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -23,17 +24,35 @@ public class UserRepository
|
||||
this._connectionString = connectionString;
|
||||
this._databaseName = "TextSender";
|
||||
this._tableName = "Users";
|
||||
var client = this.InitializeClient();
|
||||
this._bookCollection = client.GetDatabase(this._databaseName).GetCollection<User>(this._tableName);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region Metods
|
||||
public void CreateUser(User user)
|
||||
{
|
||||
var client = this.InitializeClient();
|
||||
|
||||
var coll = client.GetDatabase(this._databaseName).GetCollection<User>(this._tableName);
|
||||
coll.InsertOne(user);
|
||||
this._bookCollection.InsertOne(user);
|
||||
var i = 0;
|
||||
}
|
||||
|
||||
public List<User> RetrieveAllUsers()
|
||||
{
|
||||
return this._bookCollection.Find(_ => true).ToList();
|
||||
}
|
||||
|
||||
public void Update(User user)
|
||||
{
|
||||
var filter = Builders<User>.Filter.Eq(u => u.Id, user.Id);
|
||||
this._bookCollection.ReplaceOne(filter, user);
|
||||
}
|
||||
|
||||
public void Delete(User user)
|
||||
{
|
||||
var filter = Builders<User>.Filter.Eq(u => u.Id, user.Id);
|
||||
this._bookCollection.DeleteOne(filter);
|
||||
}
|
||||
|
||||
private MongoClient InitializeClient()
|
||||
{
|
||||
return new MongoClient(this._connectionString);
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.11" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.11" />
|
||||
<PackageReference Include="MongoDB.Driver" Version="2.21.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
|
||||
Reference in New Issue
Block a user