Niknak
Niknak

Reputation: 593

No output of data in JSON - getting 204 error code

I'm trying to output a Paymentplan for a specific customer with a specific order.

I keep getting this error:

 info:Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
  Executing ObjectResult, writing value of type 'null'.

There is something I'm doing wrong but I'm not sure what it is. Looked for solutions for a long time with no luck. I'm new to programming and C# so still trying to get hang of this. Really appreciate your help and guidance.

Here is what I have so far:

Models:

using System.Collections.Generic;

namespace Project.Models
{
    public class Customer
    {
       public string CustomerId { get; set; }
       public string FirstName { get; set; }
       public string LastName { get; set; }
       public string DateOfBirth { get; set; }
    }

   public class Order
   {
       public string OrderId { get; set; }
       public string Product { get; set; }
       public string Status { get; set; }
       public double Price { get; set; }
   }

    public class CustomerOrder
    {
      public string CustomerId {get; set;}
      public string OrderId { get; set; }
    } 

    public class Repayment
    {
      public int Amount { get; set; }
      public string DueDate { get; set; }
      public string RepaymentId { get; set; }
    }

   public class Choice
   {
      public string ChoiceId { get; set; }
      public List<Repayment> Repayments{ get; set; }
   }

   public class RepaymentPlan
   {
      public List<Choice> Choices{ get; set; }
   }
}

Repositories:

using System.Collections.Generic;
using System.Linq;
using Project.Models;
using System.Net;
using System.Collections.Specialized;
using System.Text;


namespace Project.Repositories
{
   public class OrderRepository
   {

       private static List<Order> _Orders;
       private static List<CustomerOrder> _CustomerOrder;
       private static List<RepaymentPlan> _RepaymentPlan;

       static OrderRepository()
       {
           _Orders = new List<Order>();
           _CustomerOrder= new List<CustomerOrder>();
           _PaymentPlan = new List<PaymentPlan>();

           _Orders.Add(new Order
           {
               OrderId = "124",
               Product= "Shirts",
               Status= "On it's way",
               Price= 100.20,
           });

           _Orders.Add(new Order
           {
               OrderId= "122",
               Product= "Pants",
               Status= "Not ready",
               Price= 300.30,
           });

           _Orders.Add(new Order
           {
               OrderId= "143",
               Product= "Deadpool",
               Status= "On it's way",
               Price= 6.20,
           });

           _Orders.Add(new Order
           {
               OrderId= "156",
               Product= "Socks",
               Status= "Not ready",
               Price= 3.30,
           });

           _CustomerOrder.Add(new CustomerOrder
           {
               CustomerId = "578",
               OrderId = "156",
           });

           _RepaymentPlan.Add(new RepaymentPlan 
          {
             choices = new List<Choice>
                        {
                            new Choice
                            {
                                choiceId = "cho1",
                                Repayments = new List<Repayment>
                                                {
                                                    new Repayment
                                                    {
                                                        amount = 200,
                                                        dueDate = "2018-06-01"
                                                    }, 
                                                    new Repayment
                                                    {
                                                        amount = 100,
                                                        dueDate = "2018-08-01",
                                                    }
                                                }
                            }, 
                            new Choice
                            {
                                choiceId = "cho2",
                                repayments = new List<Repayment>
                                                {
                                                    new Repayment
                                                    {
                                                        repaymentId = "Choice1",
                                                        amount = 300,
                                                        dueDate = "2018-10-01"
                                                    }, 
                                                    new Repayment
                                                    {
                                                        repaymentId = "Choice2",
                                                        amount = 150,
                                                        dueDate = "2018-11-01"
                                                    }, 
                                                }
                            }                                             
                        },
             });
}

 public static RepaymentPlan GetRepaymentPlan(string customerId, string orderId)
 {
     var customerlist =_CustomerOrder.FindAll(c => c.CustomerId==customerId); 
     var orderlist= _CustomerOrder.FindAll(c => c.orderId==orderId);
     var pplist= new RepaymentPlan();

     if (customerlist == orderlist)
     {
         return pplist;
     }
     return null;
 }

Controllers:

OrderController.cs:

using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using Project.Models;
using Project.Repositories;

namespace Project.Controllers
{
   public class OrderController : Controller
   {
      [HttpGet("customers/{customerid}/orders/{orderId}/paymentPlan")]
      public RepaymentPlan FindRepaymentPlan(string customerId, string orderId)
      {   
          return OrderRepository.GetRepaymentPlan(customerId, orderId);
      }
    }
 }

Upvotes: 0

Views: 823

Answers (1)

CynicalSection
CynicalSection

Reputation: 704

OK, after a closer scan of your code, I see that the problem is in this method

public static RepaymentPlan GetRepaymentPlan(string customerId, string orderId)

You will always return null because your condition will always evaluate to false. This is available for any (probably) programming language. if (customerlist == orderlist) evaluates if the two objects are identical (they reference the same memory address), it doesn't evaluate the content of the objects. To see if two object have the same data, you can use a loop (for, while). But since you used Linq, it is enough to use multiple conditions in FindAll function.

public static RepaymentPlan GetRepaymentPlan(string customerId, string orderId)
{
 var customerlist =_CustomerOrder.FindAll(c => c.CustomerId==customerId && c.orderId==orderId); 
 var pplist= new RepaymentPlan();

 if (customerlist.Any())
 {
     return pplist; 
 }
 return null;
}

Also, you may want to return customerlist.First() instead of pplist. If you return an empty object of type RepaymentPlan, it might not help you.

Upvotes: 2

Related Questions