From a95a921478435cc6400236d291281c3d0156a7e1 Mon Sep 17 00:00:00 2001 From: "DESKTOP-S0O2C44\\ROG" Date: Wed, 9 Mar 2022 20:42:27 +0100 Subject: Odradjena konekcija sa klasterom. Zabranjeno kreiranje korisnika sa istim imenom. #24 --- backend/api/api/Controllers/UserController.cs | 24 +++++++++++++++++++++--- backend/api/api/Services/IUserService.cs | 1 + backend/api/api/Services/UserService.cs | 6 ++++-- backend/api/api/appsettings.json | 7 +++++-- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/backend/api/api/Controllers/UserController.cs b/backend/api/api/Controllers/UserController.cs index a106880e..85f8218d 100644 --- a/backend/api/api/Controllers/UserController.cs +++ b/backend/api/api/Controllers/UserController.cs @@ -37,17 +37,35 @@ namespace api.Controllers return user; } + // GET api//5 + //potrebno za profile page + [HttpGet("{id}")] + public ActionResult GetUserUsername(string username) + { + var user = userService.GetUserUsername(username); + if (user == null) + return NotFound($"User with Id = {username} not found"); + + return user; + } // POST api/ [HttpPost] public ActionResult Post([FromBody] User user) { - userService.Create(user); + + var existingUser = userService.GetUserUsername(user.Username); - //Debug.WriteLine("\nTest.\n"); + if (existingUser != null) + return NotFound($"User with username = {user.Username} exisits"); + else + { + userService.Create(user); - return CreatedAtAction(nameof(Get), new { id = user._id }, user); + //Debug.WriteLine("\nTest.\n"); + return CreatedAtAction(nameof(Get), new { id = user._id }, user); + } } // PUT api//5 diff --git a/backend/api/api/Services/IUserService.cs b/backend/api/api/Services/IUserService.cs index e9f14c8b..b6725694 100644 --- a/backend/api/api/Services/IUserService.cs +++ b/backend/api/api/Services/IUserService.cs @@ -6,6 +6,7 @@ namespace api.Services { List Get();// daje sve korisnike User Get(string id); //daje korisnika po id-u + User GetUserUsername(string username); //daje korisnika po korisnickom imenu 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/UserService.cs b/backend/api/api/Services/UserService.cs index e5d1bb32..e1d1e8b7 100644 --- a/backend/api/api/Services/UserService.cs +++ b/backend/api/api/Services/UserService.cs @@ -19,12 +19,14 @@ namespace api.Services return user; } - - public List Get() { return _users.Find(user => true).ToList(); } + public User GetUserUsername(string username) + { + return _users.Find(user => user.Username == username).FirstOrDefault(); + } public User Get(string id) { diff --git a/backend/api/api/appsettings.json b/backend/api/api/appsettings.json index 257c2f45..204eba33 100644 --- a/backend/api/api/appsettings.json +++ b/backend/api/api/appsettings.json @@ -10,12 +10,15 @@ }, "AllowedHosts": "*", "UserStoreDatabaseSettings": { + /* LocalHost "ConnectionString": "mongodb://127.0.0.1:27017/", "DatabaseName": "si_project", "CollectionName": "User" - /* "ConnectionString": "mongodb+srv://SIDatabase:SIDatabase@sidatabase.twtfm.mongodb.net/myFirstDatabase?retryWrites=true&w=majority", + + */ + "ConnectionString": "mongodb+srv://si_user:si_user@sidatabase.twtfm.mongodb.net/myFirstDatabase?retryWrites=true&w=majority", "DatabaseName": "si_db", "CollectionName": "users" - */ + } } -- cgit v1.2.3