Web Api - Is it wrong to get a related object like this?

Asked

Viewed 46 times

0

I started developing with Web Api (Asp.net Core) and I have a doubt about this right or wrong what I am doing...

I have two classes in 1 to N:

The Department class:

public class Department
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<Seller> Sellers { get; set; } = new List<Seller>();
    }

And the Seller class:

public class Seller
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
        public DateTime BirthDate { get; set; }
        public double BaseSalary { get; set; }
        public Department Department { get; set; }
        public int DepartmentId { get; set; }
        public ICollection<SalesRecord> Sales { get; set; }
   }

And I have a third class called Dpid that inherits from Seller and has an Int property:

public class DpId : Seller
    {
        public int dpId { get; set; }

    }

My doubt appears now:

I have a Controller for Seller where in the Post method I am doing as follows to register a Seller:

 [HttpPost]
 public ActionResult<Seller> insertSeller(DpId item)
        {
            item.Department = _dpService.findById(item.dpId);
            _sellerService.insert(item);
            return NoContent();
        }

As you can see, to register a Seller, I am getting the Department for the Seller class using dpId that came in the parameter and only then I register.

Is that wrong? There’s a better way?

I started dealing with Web Api a little while ago, if you have a good tutorial could pass me (I’ve seen the web api tutorial of Docs.microsoft)

  • If I understand correctly, you just want to add a new Seller, right? Is there really a need for you to have a third entity with only one ID? I would declare the ID in the Seller entity itself. When that query is answered, I post an answer.

  • It’s because I’m adding a Seller + a Department to this Seller and not create an overload that receives with Departmentid I decided to do so, I just don’t know if this is right

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.