-2

I have the following end point on my asp.net web api that I use to post new users and receive user object from body.

// POST: api/Users
[HttpPost]
public async Task<IActionResult> PostUser([FromBody] User user)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    user.DateCreated = DateTime.Now;
    //user.LastLogin = DateTime.Now;
    var hashedPassword = BCrypt.Net.BCrypt.HashPassword(user.Password);
    user.Password = hashedPassword;
    _context.User.Add(user);
    try
    {
        await _context.SaveChangesAsync();
    }
    catch (DbUpdateException ex)
    {
        if (UserExists(user.Id))
        {
            return new StatusCodeResult(StatusCodes.Status409Conflict);
        }
        else
        {
            Console.WriteLine(ex.Message);
        }
    }

    return CreatedAtAction("GetUser", new { id = user.Id }, user);
}

One property of user is a Role object defined by the following class:

public class Role
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public DateTime DateCreated { get; set; }
    public DateTime? LastUpdate { get; set; }
}

public class User
{
    public int Id { get; set; }
    public Role role { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public DateTime? DateCreated { get; set; }
    public DateTime? LastLogin { get; set; }
}

The question is: How to I create a new user using the post endpoint? How to I pass the Role into user object? Note that I am able to create new user passing null on the role property. Now I want to pass an existing role.

This is what I am passing via Postman and angular2 app, but not getting through. return 201 status but not saving...

{
    "role": {
        "id": 1,
        "name": "Admin",
        "description": "Admin",
        "dateCreated": "2016-01-01T00:00:00",
        "lastUpdate": null
    },
    "firstName": "gggg",
    "lastName": "gggg",
    "emailAddress": "gggg",
    "userName": "ggg",
    "password": "$2a$10$z5dKRLqrus7i0fIluMynluJ8EOI2ko5vNGjBrBbfRaP738zHmc866",
    "dateCreated": "2016-08-16T21:24:18.144517+02:00",
    "lastLogin": null
}
ekad
  • 14,436
  • 26
  • 44
  • 46
abdul.badru
  • 511
  • 2
  • 6
  • 18
  • which role you want to assign to the user ? Is that coming from the client ? how ? How are you calling this endpoint ? – Shyju Aug 16 '16 at 19:12
  • From my angular2 app I am able to stringfy value from a form usinf two way databinding and get the value {"FirstName":"gggg","LastName":"gggg","EmailAddress":"gggg","UserName":"ggg","Password":"ggg","Role":{"id":1,"name":"Admin","description":"Admin","dateCreated":"2016-01-01T00:00:00","lastUpdate":null}}. But the api is not accepting this object – abdul.badru Aug 16 '16 at 19:31
  • Take a look at [How to pass json POST data to Web API method as object](http://stackoverflow.com/questions/20226169/how-to-pass-json-post-data-to-web-api-method-as-object) – Shyju Aug 16 '16 at 19:33
  • When I pass the object without the Role property it works. what am I doing wrong when I include role? – abdul.badru Aug 16 '16 at 20:07

1 Answers1

0

I have already solved this issue. Data that i was passing via body was correct (model state was valid) but there was an internal error caused my entity framework core. It was trying to recreate a role attached to the user although the role already existed.

abdul.badru
  • 511
  • 2
  • 6
  • 18