diff options
| author | DESKTOP-S0O2C44\ROG <ivan996sk@gmail.com> | 2022-03-08 17:19:37 +0100 | 
|---|---|---|
| committer | DESKTOP-S0O2C44\ROG <ivan996sk@gmail.com> | 2022-03-08 17:19:37 +0100 | 
| commit | 158c874a459b41cfacbd0238230dc5f48f481d44 (patch) | |
| tree | 371a8d27a1385fa5db8bda3eadeba065b60364b7 /backend | |
| parent | 3b9e1089b94799ee0c82fb68c71688566ab903c6 (diff) | |
Odradjena konekcija sa bazom. Odradjen kontroler za korisnika(get,post, put, delete).
Diffstat (limited to 'backend')
| -rw-r--r-- | backend/api/api/Controllers/UserController.cs | 91 | ||||
| -rw-r--r-- | backend/api/api/Data/MongoDbSettings.cs | 17 | ||||
| -rw-r--r-- | backend/api/api/Data/UserStoreDatabaseSettings.cs | 13 | ||||
| -rw-r--r-- | backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs | 9 | ||||
| -rw-r--r-- | backend/api/api/Models/User.cs | 24 | ||||
| -rw-r--r-- | backend/api/api/Program.cs | 19 | ||||
| -rw-r--r-- | backend/api/api/Services/IUserService.cs | 13 | ||||
| -rw-r--r-- | backend/api/api/Services/MongoDbService.cs | 11 | ||||
| -rw-r--r-- | backend/api/api/Services/UserService.cs | 54 | ||||
| -rw-r--r-- | backend/api/api/appsettings.json | 9 | 
10 files changed, 220 insertions, 40 deletions
| diff --git a/backend/api/api/Controllers/UserController.cs b/backend/api/api/Controllers/UserController.cs new file mode 100644 index 00000000..b1544477 --- /dev/null +++ b/backend/api/api/Controllers/UserController.cs @@ -0,0 +1,91 @@ +using api.Models; +using api.Services; +using Microsoft.AspNetCore.Mvc; +using System.Diagnostics; + +// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 +//dovrsi kontroler +namespace api.Controllers +{ +    [Route("api/[controller]")] +    [ApiController] +    public class UserController : ControllerBase +    { +        private readonly IUserService userService; + +        public UserController(IUserService userService) +        { +            this.userService = userService; +        } + +        // GET: api/<UserController> +        [HttpGet] +        public ActionResult<List<User>> Get() +        { +            return userService.Get(); +        } + +        // GET api/<UserController>/5 +        //potrebno za profile page +        [HttpGet("{id}")] +        public ActionResult<User> Get(string id) +        { +            var user = userService.Get(id); +             +            if (user == null) +                return NotFound($"User with Id = {id} not found"); +             +            return user; +        } + +        // POST api/<UserController> +        [HttpPost] +        public ActionResult<User> Post([FromBody] User user) +        { +            userService.Create(user); + +            //Debug.WriteLine("\nTest.\n"); + +            return CreatedAtAction(nameof(Get), new { id = user._id }, user); + +        } + +        // PUT api/<UserController>/5 +        [HttpPut("{id}")] +        public ActionResult Put(string id, [FromBody] User user) +        { +            var existingUser = userService.Get(id); + +            if(existingUser == null) +                return NotFound($"User with Id = {id} not found"); + +            userService.Update(id, existingUser); +            return NoContent(); +        } + +        // DELETE api/<UserController>/5 +        [HttpDelete("{id}")] +        public ActionResult Delete(string id) +        { +            var user = userService.Get(id); + +            if (user == null) +                return NotFound($"User with Id = {id} not found"); + +            userService.Delete(user._id); +            return Ok($"Student with Id = {id} deleted"); +        } +    } +} +/* +{ +    "userId": { +        "$oid": "62276146c4a20eabc664abc3" +    }, +  "username" : "ivan996sk", +  "email" : "ivan996sk@gmail.com", +  "password" : "proba", +  "firstName" : "Ivan", +  "lastName" : "Ljubisavljevic" +} +*/
\ No newline at end of file diff --git a/backend/api/api/Data/MongoDbSettings.cs b/backend/api/api/Data/MongoDbSettings.cs deleted file mode 100644 index 8b42a02c..00000000 --- a/backend/api/api/Data/MongoDbSettings.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Microsoft.EntityFrameworkCore; -using MongoDB.Driver; - -namespace api.Data -{ -    public class MongoDbSettings -    { -        public string? ConnectionURI { get; set; } = null; -        public string? DatabaseName { get; set; } = null; -        public string? CollectionName { get; set; } = null; - - - - - -    } -} diff --git a/backend/api/api/Data/UserStoreDatabaseSettings.cs b/backend/api/api/Data/UserStoreDatabaseSettings.cs new file mode 100644 index 00000000..0efd2895 --- /dev/null +++ b/backend/api/api/Data/UserStoreDatabaseSettings.cs @@ -0,0 +1,13 @@ +using Microsoft.EntityFrameworkCore; +using MongoDB.Driver; +using api.Interfaces; + +namespace api.Data +{ +    public class UserStoreDatabaseSettings : IUserStoreDatabaseSettings +    { +        public string ConnectionString { get; set; } = String.Empty; +        public string DatabaseName { get; set; } = String.Empty; +        public string CollectionName { get; set; } = String.Empty; +    } +} diff --git a/backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs b/backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs new file mode 100644 index 00000000..43fe9b3a --- /dev/null +++ b/backend/api/api/Interfaces/IUserStoreDatabaseSettings.cs @@ -0,0 +1,9 @@ +namespace api.Interfaces +{ +    public interface IUserStoreDatabaseSettings +    { +        string ConnectionString { get; set; } +        string DatabaseName { get; set; } +        string CollectionName { get; set; } +    } +} diff --git a/backend/api/api/Models/User.cs b/backend/api/api/Models/User.cs index d0f2bc0f..46db50ab 100644 --- a/backend/api/api/Models/User.cs +++ b/backend/api/api/Models/User.cs @@ -1,18 +1,28 @@  using System.ComponentModel.DataAnnotations; +using MongoDB.Bson; +using MongoDB.Bson.Serialization.Attributes; +  namespace api.Models  { +    [BsonIgnoreExtraElements]//ignorise visak elemenata iz baze --moze da se obrise jer nemamo viska      public class User      { -        [Key] -        public Guid userId { get; set; } -        public string username { get; set; } -        public string email { get; set; } -        public string password { get; set; } +        [BsonId] +        [BsonRepresentation(BsonType.ObjectId)]//mongo data type to .net +        public string _id { get; set; } +        [BsonElement("username")] +        public string Username { get; set; } +        [BsonElement("email")] +        public string Email { get; set; } +        [BsonElement("password")] +        public string Password { get; set; } -        public string firstName { get; set; } -        public int lastName { get; set; } +        [BsonElement("firstName")] +        public string FirstName { get; set; } +        [BsonElement("lastName")] +        public string LastName { get; set; }      }  } diff --git a/backend/api/api/Program.cs b/backend/api/api/Program.cs index 84514972..c54b1b3e 100644 --- a/backend/api/api/Program.cs +++ b/backend/api/api/Program.cs @@ -1,9 +1,28 @@ +using api.Data; +using api.Interfaces; +using api.Services; +using Microsoft.Extensions.Options; +using MongoDB.Driver; +  var builder = WebApplication.CreateBuilder(args);  //Add Cors  builder.Services.AddCors();  // Add services to the container +//dodajemo dep inj + +builder.Services.Configure<UserStoreDatabaseSettings>( +    builder.Configuration.GetSection(nameof(UserStoreDatabaseSettings))); + +builder.Services.AddSingleton<IUserStoreDatabaseSettings>(sp => +    sp.GetRequiredService<IOptions<UserStoreDatabaseSettings>>().Value); + +builder.Services.AddSingleton<IMongoClient>(s => +    new MongoClient(builder.Configuration.GetValue<string>("UserStoreDatabaseSettings:ConnectionString"))); + +builder.Services.AddScoped<IUserService, UserService>(); +   builder.Services.AddControllers(); diff --git a/backend/api/api/Services/IUserService.cs b/backend/api/api/Services/IUserService.cs new file mode 100644 index 00000000..e9f14c8b --- /dev/null +++ b/backend/api/api/Services/IUserService.cs @@ -0,0 +1,13 @@ +using api.Models; + +namespace api.Services +{ +    public interface IUserService +    { +        List<User> Get();// daje sve korisnike +        User Get(string id); //daje korisnika po id-u +        User Create(User user); // kreira korisnika +        void Update(string id, User user); //apdejruje korisnika po idu +        void Delete(string id);//brise korisnika +    } +} diff --git a/backend/api/api/Services/MongoDbService.cs b/backend/api/api/Services/MongoDbService.cs deleted file mode 100644 index f8b37536..00000000 --- a/backend/api/api/Services/MongoDbService.cs +++ /dev/null @@ -1,11 +0,0 @@ - - - -namespace api.Services -{ -    public class MongoDbService -    { - - -    } -} diff --git a/backend/api/api/Services/UserService.cs b/backend/api/api/Services/UserService.cs new file mode 100644 index 00000000..e5d1bb32 --- /dev/null +++ b/backend/api/api/Services/UserService.cs @@ -0,0 +1,54 @@ +using api.Interfaces; +using api.Models; +using MongoDB.Driver; + +namespace api.Services +{ +    public class UserService : IUserService  +    { +        private readonly IMongoCollection<User> _users; + +        public UserService(IUserStoreDatabaseSettings settings, IMongoClient mongoClient) +        { +            var database = mongoClient.GetDatabase(settings.DatabaseName); +             _users = database.GetCollection<User>(settings.CollectionName); +        } +        public User Create(User user) +        { +            _users.InsertOne(user); +            return user; +        } + +     + +        public List<User> Get() +        { +            return _users.Find(user => true).ToList(); +        } + +        public User Get(string id) +        { +            return _users.Find(user => user._id == id).FirstOrDefault(); +        } + +        public void Delete(string id) +        { +            _users.DeleteOne(user => user._id == id); + +        } +        public void Update(string id, User user) +        { +            _users.ReplaceOne(user => user._id == id, user); +        } +    } +} +/* + { +    "_id": "", +  "username" : "ivan996sk", +  "email" : "ivan996sk@gmail.com", +  "password" : "proba", +  "firstName" : "Ivan", +  "lastName" : "Ljubisavljevic" +} + */
\ No newline at end of file diff --git a/backend/api/api/appsettings.json b/backend/api/api/appsettings.json index b9144d93..d2c95254 100644 --- a/backend/api/api/appsettings.json +++ b/backend/api/api/appsettings.json @@ -9,10 +9,9 @@      }    },    "AllowedHosts": "*", -  "MongoDb": { -    "ConnectionURI": "mongodb+srv://LINKIKKKKKKK", -    "DatabaseName": "", -    "CollectionName": "" - +  "UserStoreDatabaseSettings": { +    "ConnectionString": "mongodb://127.0.0.1:27017/", +    "DatabaseName": "si_project", +    "CollectionName": "User"    }  } | 
