0

When I create a new record in my table I would like generate an json response with only the primary ID of my new record, somethink like : {"PrimaryID":123}

I actually use this handmade function:

    // Inserts a new row into the PatientSession table
    public string AddPatientSession(PatientSession p)
    {
        int id = (int)_dbConnection.Insert<PatientSession>(p, selectIdentity: true);
        string Idconvert = id.ToString();
        string IdInsert = "{\"PatientSessionId\":" + Idconvert + "}";
        return IdInsert;
    }

But I assume it's not the best way to do it, have you a suggestion please? Thanks in advance

Ben
  • 501
  • 6
  • 20

2 Answers2

1

If you just want to return a small JSON payload with just an Id you can use a type with only the fields you want to return, e.g:

public class AddPatientSession : IReturn<PatientId> { ... }

public class PatientId {
    public int PatientSessionId { get; set; }
}

Then use in your service like:

public class MyServices : Service
{
    public object Any(AddPatientSession request)
    {
        var model = request.ConvertTo<PatientSession>();
        return new PatientId {
            PatientSessionId = Db.Insert(model, selectIdentity: true);
        }
    }
}

Returning an object takes advantage of ServiceStack's built-in Content Negotiation to return the object serialized in the preferred Content-Type, e.g. JSON for JSON/ajax clients.

You can also return an anonymous type containing just the Id:

public object Any(AddPatientSession request)
{
    var model = request.ConvertTo<PatientSession>();
    return new {
        PatientSessionId = Db.Insert(model, selectIdentity: true);
    }
}

Which will also serialize to JSON when requested, but the lack of a type does prevent this from being called with ServiceStack's generic typed Service Clients.

mythz
  • 141,670
  • 29
  • 246
  • 390
0

Thanks you so much @mythz it's working well I just use a convert function to int because "Db.Insert" return a long type.

// Add PatientSession via POST
public class PatientSessionADD : IReturn<PatientSessionResponseId>
{
    public int PatientSessionId { get; set; }
    public int ByPatientId { get; set; }
    public DateTime PatientStartSessionTime { get; set; }
    public int PatientStartSessionByUserId { get; set; }
    public DateTime PatientEndSessionTime { get; set; }
    public int PatientEndSessionByUserId { get; set; }

}

public class PatientSessionResponseId
{
    public int PatientSessionId { get; set; }
}


public object Post(PatientSessionADD request)
    {
        var p =new PatientSession()
        {
                ByPatientId = request.ByPatientId,
                PatientStartSessionTime = request.PatientStartSessionTime,
                PatientStartSessionByUserId = request.PatientStartSessionByUserId
        };

        return new PatientSessionResponseId
        {
            PatientSessionID = Convert.ToInt16( Db.Insert<PatientSession>(p, selectIdentity: true) )
        };
    }

To resume this function get a HTTP POST message, store it in database and return a JSON response with only the Primary ID generated.

Have fun and thanks again mythz

Ben
  • 501
  • 6
  • 20
  • 1
    Note: the normal way to convert between integer types in .NET is to use an explicit cast, e.g. `(int)db.Insert(...)`. If you're going to use `Convert` use `Convert.ToInt32()` which maps to the 32bit integer type and not `short` the 16bit integer type. – mythz Jul 24 '14 at 16:48