raholsn
raholsn

Reputation: 668

Request.CreateResponse returns blank data to postman

I have encountered a problem when trying to call my web api with a post request, a empty array is returned.

My method is:

 // POST: Api/v1/transaction/
    [HttpPost]
    public HttpResponseMessage Post(string user)
    {

        var userId = new Guid(user);

      var  transactions = new Collection<TransactionDataTransferObject>();

        try
        {
            var seller = _databaseContext.Sellers.Single(s => s.Id == userId);
            var sellerMedias = _databaseContext.Medias.Where(m => m.TakenBy.Id == seller.Id);

            foreach (var sellerMedia in sellerMedias)
            {

                var allLogsForMedia = _databaseContext.Logs.Where(l => l.ObjectReferenceId == sellerMedia.Id);

                foreach (var logMedia in allLogsForMedia)
                {
                    var transaction = new TransactionDataTransferObject
                        {
                            Date = logMedia.DateTimeInUtc,
                            Amount = sellerMedia.PriceInSek,
                            MediaName = sellerMedia.FileName,
                            UserName = seller.FirstName + " " + seller.LastName
                        };

                    transactions.Add(transaction);
                }
            }
        }

        catch (Exception exception)
        {

            return Request.CreateErrorResponse(HttpStatusCode.NotFound, exception);
        }

        return Request.CreateResponse(HttpStatusCode.OK, transactions);
    }

When I debug transactions variable, I see two objects in the collection. My response to postman is

[
    {},
    {}
]

What have I done wrong? Where is my data which is sent?

Upvotes: 2

Views: 1007

Answers (1)

raholsn
raholsn

Reputation: 668

Ok, after some hours of slaming my head in the table i found out that I used [DataContract] as filter on the ViewModel,TransactionDataTransferObject.

Like this:

[DataContract]
public class TransactionDataTransferObject 
{
    [Display(Name = "Date")]
    public DateTime Date { get; set; }

    public string MediaName { get; set; }

    public Guid MediaId { get; set; }

    public string UserName { get; set; }

    public Guid UserId { get; set; }

    [Display(Name = "Description")]
    public string Discriminator { get; set; }

    [Display(Name = "Amount")]
    public decimal Amount { get; set; }    

}

Which was wrong in this case...

Thanks for reading!

Upvotes: 2

Related Questions