Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Problem
what type of return in case of user exist on database asp.net core web api ?


I make function create user on database by web API asp.net core and it working without

any problem .

I notice more users repeated so that i need to prevent user repeating records on

database .


I make function CheckUserName to check user name exist on database on table user login

if exist prevent clients from add or create new user in case of exist

My problem in case of user exist I need to return "user already exist" on return type

of calling function CheckUserName "user already exist"

but How to return user already exist message

What I have tried:

[Produces("application/json")]
    [Route("api/UserLogins")]
    public class UserLoginsController : Controller
    {
        private readonly IUserLogins _services;
        public UserLoginsController(IUserLogins Services)
        {
            this._services = Services;
        }
     }
        [HttpPost]
        public UserLogins Post([FromBody]UserLogins userDto)
        {
            if (_services.CheckUserName(userDto.UserName))
            {
return ?????????????
            }
            return  _services.Create(userDto);
          
        }
public class UserLogins:SharedValues
    {
        public int UserLoginID { get; set; }
        public string UserName { get; set; }
        public string UserMail { get; set; }
        public string UserPass { get; set; }
        public int FkTeamID { get; set; }
        public Boolean IsAdmin { get; set; }


    }
 
 public interface IUserLogins
    {
        UserLogins Create(UserLogins user);
        bool CheckUserName(string username);
    }
Posted
Updated 21-Jan-20 4:35am
v2

The way I would go about it is to have separate functions for checking if a use exists and crating a new user.

The calling page would perform the "check" function as a validation step; if it exists ty simply let the customer know the account already exists and ask if they want to go to the login page.
If the account does not exist; then you can go into the create function seamlessly without letting the end user known.

If you are having problems with users making multiple accounts with the same credentials, it would seem that there may be a few other problems; generally the database table would have a Primary Key or a Unique Index on the username field which would return an error if you did try to create a new row with the same index/key value.
The other thing I would look at is the user interface; and asking why so many people are trying to create multiple accounts.
 
Share this answer
 
You've tagged the question as ASP.NET Core; I'm assuming you're using at least v2.1?

Change the action return type to ActionResult<UserLogins>, which will let you return either the newly created user, or an HTTP status code.
C#
[HttpPost]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status409Conflict)]
public ActionResult<UserLogins> Post([FromBody] UserLogins userDto)
{
    if (_services.CheckUserName(userDto.UserName))
    {
        return StatusCode(409, $"User '{userDto.UserName}' already exists.");
    }
    
    return  _services.Create(userDto);      
}
Controller action return types in ASP.NET Core web API | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900