Example #1
0
        public async Task <IActionResult> Post([FromBody] Customer customer)
        {
            if (customer == null)
            {
                return(BadRequest());
            }

            _context.Customer.Add(customer);
            await _context.SaveChangesAsync();

            // This loads the nested details object onto the customer object, without it customer.details is null
            _context.Entry(customer).Reference(c => c.details).Load();

            CustomerDTO customerDto = new CustomerDTO()
            {
                Id      = customer.customer_id,
                Name    = customer.name,
                Email   = customer.email,
                Phone   = customer.phone,
                Age     = customer.age,
                Details = customer.details
            };

            return(CreatedAtRoute("GetCustomer", new { id = customer.customer_id }, customerDto));
        }
Example #2
0
        public async Task <IActionResult> Post([FromBody] Lead lead)
        {
            if (lead == null)
            {
                return(BadRequest());
            }

            _context.Lead.Add(lead);
            await _context.SaveChangesAsync();

            _context.Entry(lead).Reference(l => l.status).Load();
            _context.Entry(lead).Reference(l => l.priority).Load();
            _context.Entry(lead).Reference(l => l.customer).Load();
            // _context.Entry(lead).Reference(l => l.customer.details).Load(); <- figure this out, not working throws a 500
            _context.Entry(lead).Reference(l => l.employee).Load();

            LeadDTO leadDto = new LeadDTO()
            {
                Id          = lead.lead_id,
                LastContact = lead.last_contact,
                Status      = lead.status,
                Priority    = lead.priority,
                Customer    = new CustomerDTO()
                {
                    Id      = lead.customer.customer_id,
                    Name    = lead.customer.name,
                    Email   = lead.customer.email,
                    Phone   = lead.customer.phone,
                    Age     = lead.customer.age,
                    Details = lead.customer.details
                },
                Employee = new EmployeeDTO()
                {
                    Id    = lead.employee.employee_id,
                    Name  = lead.employee.name,
                    Email = lead.employee.email,
                    Phone = lead.employee.phone
                }
            };

            return(CreatedAtRoute("GetLead", new { id = lead.lead_id }, leadDto));
        }
Example #3
0
        public async Task <IActionResult> Post([FromBody] Employee employee)
        {
            if (employee == null)
            {
                return(BadRequest());
            }

            _context.Employee.Add(employee);
            await _context.SaveChangesAsync();

            EmployeeDTO employeeDto = new EmployeeDTO()
            {
                Id    = employee.employee_id,
                Name  = employee.name,
                Email = employee.email,
                Phone = employee.phone
            };

            return(CreatedAtRoute("GetEmployee", new { id = employee.employee_id }, employeeDto));
        }