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);
        }
Beispiel #2
0
        public HttpResponseMessage Put(HttpRequestMessage request, ComputerViewModel computerVm)
        {
            return(CreateHttpResponse(request, () =>
            {
                HttpResponseMessage errorResponse;
                if (AddUpdateComputerValidation(request, computerVm, out errorResponse))
                {
                    return errorResponse;
                }

                var computerDb = _computerService.GetById(computerVm.ComputerId);
                computerDb.UpdateComputer(computerVm);
                _computerService.Update(computerDb);
                _computerService.Save();

                var response = request.CreateResponse(HttpStatusCode.OK);
                return response;
            }));
        }