diff options
-rw-r--r-- | backend/api/api/Controllers/DatasetController.cs | 4 | ||||
-rw-r--r-- | backend/api/api/Controllers/ModelController.cs | 74 | ||||
-rw-r--r-- | backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs | 1 | ||||
-rw-r--r-- | backend/api/api/Models/Dataset.cs | 2 | ||||
-rw-r--r-- | backend/api/api/Program.cs | 2 | ||||
-rw-r--r-- | backend/api/api/Services/IModelService.cs | 15 | ||||
-rw-r--r-- | backend/api/api/Services/ModelService.cs | 47 | ||||
-rw-r--r-- | backend/api/api/appsettings.json | 6 |
8 files changed, 144 insertions, 7 deletions
diff --git a/backend/api/api/Controllers/DatasetController.cs b/backend/api/api/Controllers/DatasetController.cs index fcebc4b0..8ad92149 100644 --- a/backend/api/api/Controllers/DatasetController.cs +++ b/backend/api/api/Controllers/DatasetController.cs @@ -41,9 +41,9 @@ namespace api.Controllers [HttpPost("post")] public ActionResult<Dataset> Post([FromBody] Dataset dataset) { - var existingUser = _datasetService.GetOneDataset(dataset.uploaderId,dataset.name); + var existingDataset = _datasetService.GetOneDataset(dataset.uploaderId,dataset.name); - if (existingUser != null) + if (existingDataset != null) return NotFound($"Dateset with name = {dataset.name} exisits"); else { diff --git a/backend/api/api/Controllers/ModelController.cs b/backend/api/api/Controllers/ModelController.cs index 5e22c61d..ac45f45b 100644 --- a/backend/api/api/Controllers/ModelController.cs +++ b/backend/api/api/Controllers/ModelController.cs @@ -1,4 +1,5 @@ -using api.Services; +using api.Models; +using api.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -11,10 +12,13 @@ namespace api.Controllers { private IMlConnectionService _mlService; + private readonly IModelService _modelService; + - public ModelController(IMlConnectionService mlService) + public ModelController(IMlConnectionService mlService, IModelService modelService) { _mlService = mlService; + _modelService = modelService; } [HttpPost("sendModel")] @@ -25,5 +29,71 @@ namespace api.Controllers return Ok(result); } + //id korisnika + // GET: api/<ModelController>/{id}/datasets + [HttpGet("{id}/models")] + public ActionResult<List<Model>> Get(string id) + { + return _modelService.GetAllModels(id); + } + + //id korisnika, name modela + // GET api/<ModelController>/{id}/{name} + [HttpGet("{id}/{name}")] + public ActionResult<Model> Get(string id, string name) + { + var model = _modelService.GetOneModel(id, name); + + if (model == null) + return NotFound($"Model with name = {name} or user with id = {id} not found"); + + return model; + } + + // POST api/<ModelController>/post + [HttpPost("post")] + public ActionResult<Model> Post([FromBody] Model model) + { + var existingModel = _modelService.GetOneModel(model.uploaderId, model.name); + + if (existingModel != null) + return NotFound($"Model with name = {model.name} exisits"); + else + { + _modelService.Create(model); + + return CreatedAtAction(nameof(Get), new { id = model._id }, model); + } + } + + // PUT api/<ModelController>/{id}/{name} + [HttpPut("{id}/{name}")] + public ActionResult Put(string id, string name, [FromBody] Model model) + { + var existingModel = _modelService.GetOneModel(id, name); + + //ne mora da se proverava + if (existingModel == null) + return NotFound($"Model with name = {name} or user with id = {id} not found"); + + _modelService.Update(id, name, model); + return NoContent(); + } + + // DELETE api/<ModelController>/5 + [HttpDelete("{id}")] + public ActionResult Delete(string id, string name) + { + var model = _modelService.GetOneModel(id, name); + + if (model == null) + return NotFound($"Model with name = {name} or user with id = {id} not found"); + + _modelService.Delete(model.uploaderId, model.name); + + return Ok($"Model with name = {name} deleted"); + + } + } } diff --git a/backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs b/backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs index 8d2a175f..46ece53c 100644 --- a/backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs +++ b/backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs @@ -6,5 +6,6 @@ string DatabaseName { get; set; } string CollectionName { get; set; } string DatasetCollectionName { get; set; } + string ModelCollectionName { get; } } } diff --git a/backend/api/api/Models/Dataset.cs b/backend/api/api/Models/Dataset.cs index d58b3143..de88fd4f 100644 --- a/backend/api/api/Models/Dataset.cs +++ b/backend/api/api/Models/Dataset.cs @@ -13,7 +13,7 @@ namespace api.Models public string _id { get; set; } public string name { get; set; } public string description { get; set; } - public string header { get; set; } + public string[] header { get; set; } public int fileId { get; set; } public string extension { get; set; } public bool isPublic { get; set; } diff --git a/backend/api/api/Program.cs b/backend/api/api/Program.cs index 702ef259..24cc5cfe 100644 --- a/backend/api/api/Program.cs +++ b/backend/api/api/Program.cs @@ -29,6 +29,8 @@ builder.Services.AddScoped<IDatasetService, DatasetService>(); builder.Services.AddScoped<IUserService, UserService>(); builder.Services.AddScoped<IAuthService, AuthService>(); builder.Services.AddScoped<IMlConnectionService, MlConnectionService>(); +builder.Services.AddScoped<IModelService, ModelService>(); + //Add Authentication builder.Services.AddAuthentication( diff --git a/backend/api/api/Services/IModelService.cs b/backend/api/api/Services/IModelService.cs new file mode 100644 index 00000000..988f3ab9 --- /dev/null +++ b/backend/api/api/Services/IModelService.cs @@ -0,0 +1,15 @@ +using System; +using api.Models; + +namespace api.Services +{ + public interface IModelService + { + Model GetOneModel(string uploaderId, string name); + List<Model> GetAllModels(string uploaderId); + Model Create(Model model); + void Update(string uploaderId, string name, Model model); + void Delete(string uploaderId, string name); + } +} + diff --git a/backend/api/api/Services/ModelService.cs b/backend/api/api/Services/ModelService.cs new file mode 100644 index 00000000..97402d90 --- /dev/null +++ b/backend/api/api/Services/ModelService.cs @@ -0,0 +1,47 @@ +using System; +using api.Interfaces; +using api.Models; +using MongoDB.Driver; + +namespace api.Services +{ + public class ModelService : IModelService + { + + private readonly IMongoCollection<Model> _model; + + public ModelService(IUserStoreDatabaseSettings settings, IMongoClient mongoClient) + { + var database = mongoClient.GetDatabase(settings.DatabaseName); + _model = database.GetCollection<Model>(settings.ModelCollectionName); + } + + public Model Create(Model model) + { + _model.InsertOne(model); + return model; + } + + public void Delete(string uploaderId, string name) + { + _model.DeleteOne(model => (model.uploaderId == uploaderId && model.name == name)); + } + + public List<Model> GetAllModels(string uploaderId) + { + return _model.Find(model => model.uploaderId == uploaderId).ToList(); + } + + public Model GetOneModel(string uploaderId, string name) + { + return _model.Find(model => model.uploaderId == uploaderId && model.name == name).FirstOrDefault(); + } + + public void Update(string uploaderId, string name, Model model) + { + _model.ReplaceOne(model => model.uploaderId == uploaderId && model.name == name, model); + } + + } +} + diff --git a/backend/api/api/appsettings.json b/backend/api/api/appsettings.json index 9b4f00a3..63030c15 100644 --- a/backend/api/api/appsettings.json +++ b/backend/api/api/appsettings.json @@ -14,11 +14,13 @@ "ConnectionString": "mongodb://127.0.0.1:27017/", "DatabaseName": "si_project", "CollectionName": "User", - "DatasetCollectionName" : "Dataset" + "DatasetCollectionName" : "Dataset", + "ModelCollectionName" : "Model" */ "ConnectionString": "mongodb+srv://si_user:si_user@sidatabase.twtfm.mongodb.net/myFirstDatabase?retryWrites=true&w=majority", "DatabaseName": "si_db", "CollectionName": "users", - "DatasetCollectionName": "Dataset" + "DatasetCollectionName": "Dataset", + "ModelCollectionName": "Model" } } |