public async Task <ActionResult <ComputerModel> > PutComputer(Guid id, ComputerModel computer)
        {
            if (id != computer.Id)
            {
                return(BadRequest());
            }

            var existingComputer = await _computerService.Get(id);

            if (existingComputer == null)
            {
                return(NotFound());
            }

            ComputerModel updatedComputer;

            switch (existingComputer.TypeName)
            {
            case "Desktop":
                PCComputer pcComputerToUpdate = computer as PCComputer;
                pcComputerToUpdate = new PCComputer {
                    Id            = pcComputerToUpdate.Id,
                    Brand         = pcComputerToUpdate.Brand,
                    ProcessorName = pcComputerToUpdate.ProcessorName,
                    Quantity      = pcComputerToUpdate.Quantity,
                    RamSlots      = pcComputerToUpdate.RamSlots,
                    UsbPorts      = pcComputerToUpdate.UsbPorts
                };
                updatedComputer = pcComputerToUpdate;
                break;

            case "Laptop":
                LaptopComputer lpComputerToUpdate = computer as LaptopComputer;
                lpComputerToUpdate = new LaptopComputer {
                    Id            = lpComputerToUpdate.Id,
                    Brand         = lpComputerToUpdate.Brand,
                    ProcessorName = lpComputerToUpdate.ProcessorName,
                    ScreenSize    = lpComputerToUpdate.ScreenSize
                };
                updatedComputer = lpComputerToUpdate;
                break;

            default:
                return(BadRequest());
            }

            await _computerService.Update(updatedComputer);

            return(existingComputer);
        }
示例#2
0
        public async void Test_PC_Storing_And_Unboxing()
        {
            //Test that when we store a PC object via its parent class, we can unbox the return value to an
            //instance of the PC class and we don't just get a truncated parent class.

            PCComputer pc = new PCComputer {
                Brand = "Dell", ProcessorName = "Pentium", Quantity = 1, RamSlots = 2, UsbPorts = 2
            };

            ActionResult <ComputerModel> result = await cp.PostComputer(pc);

            var res = result.Value as PCComputer;

            Assert.Equal(pc, res);
            Assert.Equal(2, res.RamSlots); //Has specific property of PC;
        }
        public async Task <ActionResult <ComputerModel> > PostComputer(GenericComputer computer)
        {
            if (computer == null)
            {
                return(BadRequest());
            }

            ComputerModel cp;

            switch (computer.TypeName)
            {
            case "Desktop":
                PCComputer newPC = new PCComputer {
                    Id            = Guid.NewGuid(),
                    TypeName      = computer.TypeName,
                    Brand         = computer.Brand,
                    ProcessorName = computer.ProcessorName,
                    Quantity      = computer.Quantity,
                    UsbPorts      = computer.UsbPorts,
                    RamSlots      = computer.RamSlots,
                    ImageUrl      = computer.ImageUrl,
                };
                cp = newPC;
                break;

            case "Laptop":
                LaptopComputer newLP = new LaptopComputer {
                    Id            = Guid.NewGuid(),
                    TypeName      = computer.TypeName,
                    Brand         = computer.Brand,
                    ProcessorName = computer.ProcessorName,
                    Quantity      = computer.Quantity,
                    ImageUrl      = computer.ImageUrl,
                    ScreenSize    = computer.ScreenSize
                };
                cp = newLP;
                break;

            default:
                return(BadRequest());
            }

            await _computerService.Add(cp);

            return(cp);
        }
示例#4
0
        public async void Test_PC_Storing_And_Update()
        {
            PCComputer pc = new PCComputer {
                Brand = "Dell", ProcessorName = "Pentium", Quantity = 1, RamSlots = 2, UsbPorts = 2
            };

            await cp.PostComputer(pc);

            var p = await cp.GetComputer(pc.Id);

            var res = p.Value as PCComputer;

            res.UsbPorts = 4;

            await cp.PutComputer(res.Id, res);

            var p2 = await cp.GetComputer(res.Id);

            var res2 = p2.Value as PCComputer;

            Assert.Equal(pc, res);                     //New PC and inserted PC are identical.
            Assert.Equal(res.UsbPorts, res2.UsbPorts); //Updated PC has correct number of slots.
        }