diff options
Diffstat (limited to 'backend/api')
-rw-r--r-- | backend/api/api/Controllers/AuthController.cs | 23 | ||||
-rw-r--r-- | backend/api/api/Controllers/DatasetController.cs | 112 | ||||
-rw-r--r-- | backend/api/api/Controllers/UserController.cs | 2 | ||||
-rw-r--r-- | backend/api/api/Data/UserStoreDatabaseSettings.cs | 1 | ||||
-rw-r--r-- | backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs | 1 | ||||
-rw-r--r-- | backend/api/api/Models/Dataset.cs | 46 | ||||
-rw-r--r-- | backend/api/api/Models/JwtToken.cs | 30 | ||||
-rw-r--r-- | backend/api/api/Program.cs | 1 | ||||
-rw-r--r-- | backend/api/api/Services/AuthService.cs | 15 | ||||
-rw-r--r-- | backend/api/api/Services/DatasetService.cs | 43 | ||||
-rw-r--r-- | backend/api/api/Services/IAuthService.cs | 1 | ||||
-rw-r--r-- | backend/api/api/Services/IDatasetService.cs | 14 | ||||
-rw-r--r-- | backend/api/api/Services/UserService.cs | 13 | ||||
-rw-r--r-- | backend/api/api/appsettings.json | 5 |
14 files changed, 302 insertions, 5 deletions
diff --git a/backend/api/api/Controllers/AuthController.cs b/backend/api/api/Controllers/AuthController.cs index 01354f87..6dfe483a 100644 --- a/backend/api/api/Controllers/AuthController.cs +++ b/backend/api/api/Controllers/AuthController.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using api.Services; using Microsoft.AspNetCore.Authorization; +using Microsoft.Net.Http.Headers; namespace api.Controllers { @@ -30,6 +31,28 @@ namespace api.Controllers return Ok(_auth.Login(user)); } + [HttpGet("Auth")] + [Authorize(Roles ="User")] + public async Task<ActionResult<string>> TestAuth() + { + return Ok("works"); + } + + [HttpPost("renewJwt")] + [Authorize(Roles = "User")] + public async Task<ActionResult<string>> RenewJwt() { + var authorization = Request.Headers[HeaderNames.Authorization]; + + var newToken=_auth.RenewToken(authorization); + if(newToken== null) + return BadRequest(); + return Ok(newToken); + + + + + } + } diff --git a/backend/api/api/Controllers/DatasetController.cs b/backend/api/api/Controllers/DatasetController.cs new file mode 100644 index 00000000..d022d6d2 --- /dev/null +++ b/backend/api/api/Controllers/DatasetController.cs @@ -0,0 +1,112 @@ +using api.Models; +using api.Services; +using Microsoft.AspNetCore.Mvc; + +// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 + +namespace api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class DatasetController : ControllerBase + { + private readonly IDatasetService _datasetService; + + public DatasetController(IDatasetService datasetService) + { + _datasetService = datasetService; + } + + + // GET: api/<DatasetController>/{id}/datasets + [HttpGet("{id}/datasets")] + public ActionResult<List<Dataset>> Get(string id) + { + return _datasetService.GetAllDatesets(id); + } + + // GET api/<DatasetController>/{id}/{name} + [HttpGet("{id}/{name}")] + public ActionResult<Dataset> Get(string id, string name) + { + var dataset = _datasetService.GetOneDataset(id, name); + + if (dataset == null) + return NotFound($"Dataset with name = {name} not found"); + + return dataset; + } + + // POST api/<DatasetController> + [HttpPost("post")] + public ActionResult<Dataset> Post([FromBody] Dataset dataset) + { + var existingUser = _datasetService.GetOneDataset(dataset.uploaderId,dataset.name); + + if (existingUser != null) + return NotFound($"Dateset with name = {dataset.name} exisits"); + else + { + _datasetService.Create(dataset); + + return CreatedAtAction(nameof(Get), new { id = dataset._id }, dataset); + } + } + + // PUT api/<DatasetController>/5 + [HttpPut("{id}/{name}")] + public ActionResult Put(string id, string name, [FromBody] Dataset dataset) + { + var existingDataset = _datasetService.GetOneDataset(id, name); + + //ne mora da se proverava + if (existingDataset == null) + return NotFound($"Dataset with name = {name} not found"); + + _datasetService.Update(id, name, dataset); + return NoContent(); + } + + // DELETE api/<DatasetController>/5 + [HttpDelete("{id}")] + public ActionResult Delete(string id, string name) + { + var dataset = _datasetService.GetOneDataset(id, name); + + if (dataset == null) + return NotFound($"Dataset with name = {name} not found"); + + _datasetService.Delete(dataset.uploaderId,dataset.name); + + return Ok($"Dataset with name = {name} deleted"); + + } + } +} + +/* +{ + "_id": "", + "uploaderId" : "uploaderId", + "name" : "name", + "description" : "description", + "dateCreated" : "dateCreated", + "inputColumns" : [2,3,4], + "columnToPredict" : 1, + "randomTestSet" : true, + "randomTestSetDistribution" : 1, + "type" : "type", + "encoding" : "encoding", + "optimizer" : "optimizer", + "lossFunction" : "lossFunction", + "inputNeurons" : 2, + "hiddenLayerNeurons" : 3, + "hiddenLayers" : 8, + "batchSize" : 6, + "inputLayerActivationFunction" : "inputLayerActivationFunction", + "hiddenLayerActivationFunction" : "hiddenLayerActivationFunction", + "outputLayerActivationFunction" : "outputLayerActivationFunction", + "extension" : "extension" + +} +*/
\ No newline at end of file diff --git a/backend/api/api/Controllers/UserController.cs b/backend/api/api/Controllers/UserController.cs index 85f8218d..d41a42e3 100644 --- a/backend/api/api/Controllers/UserController.cs +++ b/backend/api/api/Controllers/UserController.cs @@ -37,6 +37,7 @@ namespace api.Controllers return user; } + /* // GET api/<UserController>/5 //potrebno za profile page [HttpGet("{id}")] @@ -49,6 +50,7 @@ namespace api.Controllers return user; } + */ // POST api/<UserController> [HttpPost] public ActionResult<User> Post([FromBody] User user) diff --git a/backend/api/api/Data/UserStoreDatabaseSettings.cs b/backend/api/api/Data/UserStoreDatabaseSettings.cs index 0efd2895..0d923fc7 100644 --- a/backend/api/api/Data/UserStoreDatabaseSettings.cs +++ b/backend/api/api/Data/UserStoreDatabaseSettings.cs @@ -9,5 +9,6 @@ namespace api.Data public string ConnectionString { get; set; } = String.Empty; public string DatabaseName { get; set; } = String.Empty; public string CollectionName { get; set; } = String.Empty; + public string DatasetCollectionName { get; set; } = String.Empty; } } diff --git a/backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs b/backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs index 43fe9b3a..8d2a175f 100644 --- a/backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs +++ b/backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs @@ -5,5 +5,6 @@ string ConnectionString { get; set; } string DatabaseName { get; set; } string CollectionName { get; set; } + string DatasetCollectionName { get; set; } } } diff --git a/backend/api/api/Models/Dataset.cs b/backend/api/api/Models/Dataset.cs new file mode 100644 index 00000000..0dc87f40 --- /dev/null +++ b/backend/api/api/Models/Dataset.cs @@ -0,0 +1,46 @@ +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; + +namespace api.Models +{ + public class Dataset + { + internal string uploaderId; + + [BsonId] + [BsonRepresentation(BsonType.ObjectId)]//mongo data type to .net + public string _id { get; set; } + [BsonElement("uploaderId")] + public string UploaderId { get; set; } + [BsonElement("name")] + public string name { get; set; } + + public string description { get; set; } + //datetime + public string dateCreated { get; set; } + + public int[] inputColumns { get; set; } + public int columnToPredict { get; set; } + public bool randomTestSet { get; set; } + public int randomTestSetDistribution { get; set; } + + + public string type { get; set; } + public string encoding { get; set; } + public string optimizer { get; set; } + public string lossFunction { get; set; } + public int inputNeurons { get; set; } + public int hiddenLayerNeurons { get; set; } + public int hiddenLayers { get; set; } + public int batchSize { get; set; } + public string inputLayerActivationFunction { get; set; } + public string hiddenLayerActivationFunction { get; set; } + public string outputLayerActivationFunction { get; set; } + + + [BsonElement("extension")] + public string extension { get; set; } + + + } +} diff --git a/backend/api/api/Models/JwtToken.cs b/backend/api/api/Models/JwtToken.cs index 23307185..3ecbf92d 100644 --- a/backend/api/api/Models/JwtToken.cs +++ b/backend/api/api/Models/JwtToken.cs @@ -31,6 +31,36 @@ namespace api.Models } + public string RenewToken(string existingToken) + { + if (existingToken == null) + return null; + var tokenHandler = new JwtSecurityTokenHandler(); + var key= Encoding.ASCII.GetBytes(_configuration.GetSection("AppSettings:JwtToken").Value); + try + { + tokenHandler.ValidateToken(existingToken, new TokenValidationParameters + { + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey(key), + ValidateIssuer = false, + ValidateAudience = false, + }, out SecurityToken validatedToken); + + var jwtToken = (JwtSecurityToken)validatedToken; + var userName =jwtToken.Claims.First(x => x.Type == "name").Value; + var authUser = new AuthRequest(); + authUser.UserName = userName; + + return GenToken(authUser); + } + catch + { + return null; + } + + } + } diff --git a/backend/api/api/Program.cs b/backend/api/api/Program.cs index 550f6ce1..2c569daf 100644 --- a/backend/api/api/Program.cs +++ b/backend/api/api/Program.cs @@ -25,6 +25,7 @@ builder.Services.AddSingleton<IMongoClient>(s => new MongoClient(builder.Configuration.GetValue<string>("UserStoreDatabaseSettings:ConnectionString"))); //Inject Dependencies +builder.Services.AddScoped<IDatasetService, DatasetService>(); builder.Services.AddScoped<IUserService, UserService>(); builder.Services.AddScoped<IAuthService, AuthService>(); diff --git a/backend/api/api/Services/AuthService.cs b/backend/api/api/Services/AuthService.cs index 015fdac7..4f838463 100644 --- a/backend/api/api/Services/AuthService.cs +++ b/backend/api/api/Services/AuthService.cs @@ -1,4 +1,5 @@ -using api.Interfaces; +using System.Net.Http.Headers; +using api.Interfaces; using api.Models; using api.Models.Users; using MongoDB.Driver; @@ -44,6 +45,18 @@ namespace api.Services return "User added"; } + public string RenewToken(string header) + { + if (AuthenticationHeaderValue.TryParse(header, out var headerValue)) + { + + var scheme = headerValue.Scheme; + var parameter = headerValue.Parameter; + return _jwt.RenewToken(parameter); + } + return null; + } + } } diff --git a/backend/api/api/Services/DatasetService.cs b/backend/api/api/Services/DatasetService.cs new file mode 100644 index 00000000..1b6d22be --- /dev/null +++ b/backend/api/api/Services/DatasetService.cs @@ -0,0 +1,43 @@ +using api.Interfaces; +using api.Models; +using MongoDB.Driver; + +namespace api.Services +{ + public class DatasetService : IDatasetService + { + private readonly IMongoCollection<Dataset> _dataset; + + public DatasetService(IUserStoreDatabaseSettings settings, IMongoClient mongoClient) + { + var database = mongoClient.GetDatabase(settings.DatabaseName); + _dataset = database.GetCollection<Dataset>(settings.DatasetCollectionName); + } + //kreiranje dataseta + public Dataset Create(Dataset dataset) + { + _dataset.InsertOne(dataset); + return dataset; + } + + //brisanje odredjenog name-a + public void Delete(string uploaderId, string name) + { + _dataset.DeleteOne(dataset => (dataset.UploaderId == uploaderId && dataset.name == name)); + } + public List<Dataset> GetAllDatesets(string uploaderId) + { + return _dataset.Find(dataset => dataset.uploaderId == uploaderId).ToList(); + } + public Dataset GetOneDataset(string uploaderId, string name) + { + return _dataset.Find(dataset => dataset.UploaderId == uploaderId && dataset.name == name).FirstOrDefault(); + } + + //ako je potrebno da se zameni name ili ekstenzija + public void Update(string uploaderId, string name, Dataset dataset) + { + _dataset.ReplaceOne(dataset => dataset.UploaderId == uploaderId && dataset.name == name, dataset); + } + } +} diff --git a/backend/api/api/Services/IAuthService.cs b/backend/api/api/Services/IAuthService.cs index 79085f8c..591d122d 100644 --- a/backend/api/api/Services/IAuthService.cs +++ b/backend/api/api/Services/IAuthService.cs @@ -6,5 +6,6 @@ namespace api.Services { string Login(AuthRequest user); string Register(RegisterRequest user); + string RenewToken(string token); } }
\ No newline at end of file diff --git a/backend/api/api/Services/IDatasetService.cs b/backend/api/api/Services/IDatasetService.cs new file mode 100644 index 00000000..9cf8c3cb --- /dev/null +++ b/backend/api/api/Services/IDatasetService.cs @@ -0,0 +1,14 @@ + +using api.Models; + +namespace api.Services +{ + public interface IDatasetService + { + Dataset GetOneDataset(string uploaderId, string name); + List<Dataset> GetAllDatesets(string uploaderId); + Dataset Create(Dataset dataset); + void Update(string uploaderId, string name, Dataset dataset); + void Delete(string uploaderId, string name); + } +} diff --git a/backend/api/api/Services/UserService.cs b/backend/api/api/Services/UserService.cs index e1d1e8b7..c626889d 100644 --- a/backend/api/api/Services/UserService.cs +++ b/backend/api/api/Services/UserService.cs @@ -23,6 +23,7 @@ namespace api.Services { return _users.Find(user => true).ToList(); } + public User GetUserUsername(string username) { return _users.Find(user => user.Username == username).FirstOrDefault(); @@ -53,4 +54,14 @@ namespace api.Services "firstName" : "Ivan", "lastName" : "Ljubisavljevic" } - */
\ No newline at end of file + +{ + "_id": { + "$oid": "62291140d88e6bcf95c96a58" + }, + "uploaderId":"", + "extension" : "", + "name" : "" +} + +*/ diff --git a/backend/api/api/appsettings.json b/backend/api/api/appsettings.json index 204eba33..3661f171 100644 --- a/backend/api/api/appsettings.json +++ b/backend/api/api/appsettings.json @@ -14,11 +14,10 @@ "ConnectionString": "mongodb://127.0.0.1:27017/", "DatabaseName": "si_project", "CollectionName": "User" - */ "ConnectionString": "mongodb+srv://si_user:si_user@sidatabase.twtfm.mongodb.net/myFirstDatabase?retryWrites=true&w=majority", "DatabaseName": "si_db", - "CollectionName": "users" - + "CollectionName": "users", + "DatasetCollectionName" : "Dataset" } } |