diff options
-rw-r--r-- | backend/api/api/Controllers/UserController.cs | 24 | ||||
-rw-r--r-- | backend/api/api/Services/IUserService.cs | 1 | ||||
-rw-r--r-- | backend/api/api/Services/UserService.cs | 6 | ||||
-rw-r--r-- | 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/<UserController>/5 + //potrebno za profile page + [HttpGet("{id}")] + public ActionResult<User> GetUserUsername(string username) + { + var user = userService.GetUserUsername(username); + if (user == null) + return NotFound($"User with Id = {username} not found"); + + return user; + } // POST api/<UserController> [HttpPost] public ActionResult<User> 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/<UserController>/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<User> 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<User> 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" - */ + } } |