public ActionResult <Product> UpdateService([FromBody] ServiceUpdateDto serviceUpdateDto, Guid serviceId, [FromHeader] Guid accountId, [FromHeader] string key) { try { if (!auth.AuthorizeUser(key)) { return(StatusCode(StatusCodes.Status401Unauthorized, "Authorization failed!")); } if (serviceUpdateDto.AccountId != accountId) { return(StatusCode(StatusCodes.Status403Forbidden, "Not allowed! User does not have permission!")); } var oldService = serviceRepository.GetServiceById(serviceId); if (oldService == null) { return(NotFound()); } Service newService = mapper.Map <Service>(serviceUpdateDto); serviceRepository.UpdateService(oldService, newService); serviceRepository.SaveChanges(); logger.Log(LogLevel.Information, contextAccessor.HttpContext.TraceIdentifier, "", "Product updated", null); return(Ok(oldService)); } catch (Exception ex) { logger.Log(LogLevel.Error, contextAccessor.HttpContext.TraceIdentifier, "", "Update error", null); return(StatusCode(StatusCodes.Status500InternalServerError, ex.Message)); } }
public void UpdateService_ValidObjectPassed_ReturnsNoContentResult() { int serviceId = 2; var testItem = new ServiceUpdateDto() { Shortname = "PHONE", Name = "Phone service" }; var response = controller.UpdateService(serviceId, testItem); Assert.IsType <NoContentResult>(response); }
public async Task <IActionResult> PutAsync([FromRoute] int id, [FromBody] ServiceUpdateDto serviceUpdateDTO) { var service = _mapper.Map <Service>(serviceUpdateDTO); var updated = await _serviceService.UpdateAsync(id, service); if (!updated) { return(NotFound()); } return(NoContent()); }
public void UpdateService_InvalidObjectPassed_ReturnsBadRequestResult() { int serviceId = 2; var testItem = new ServiceUpdateDto() { Shortname = "", Name = "" }; controller.ModelState.AddModelError("Shortname", "Required"); var response = controller.UpdateService(serviceId, testItem); Assert.IsType <BadRequestResult>(response); }
public async Task <ActionResult> Update(ServiceUpdateDto serviceUpdateDto) { var service = new Service(); _mapper.Map(serviceUpdateDto, service); _serviceRepository.Update(service); if (await _serviceRepository.SaveAllAsync()) { return(NoContent()); } return(BadRequest("Ocurrió un error al actualizar el servicio")); }
public async Task <ActionResult <Department> > Update(int id, [FromBody] ServiceUpdateDto serviceUpdateDto) { if (id != serviceUpdateDto.Id) { return(BadRequest()); } Service dbservice = _context.Services.FirstOrDefault(p => p.Id == id); if (dbservice == null) { return(NotFound()); } dbservice.Name = serviceUpdateDto.Name; dbservice.Description = serviceUpdateDto.Description; dbservice.ShortDesc = serviceUpdateDto.ShortDesc; await _context.SaveChangesAsync(); return(Ok(dbservice)); }
public ActionResult UpdateService(int id, ServiceUpdateDto serviceUpdateDto) { if (serviceUpdateDto.Shortname.Equals("")) { return(BadRequest()); } var serviceEntity = repository.GetServiceById(id); if (serviceEntity == null) { return(NotFound()); } mapper.Map(serviceUpdateDto, serviceEntity); repository.UpdateService(serviceEntity); repository.SaveChanges(); return(NoContent()); }
public void UpdateService_ValidObjectPassed_ReturnedResponseHasUpdatedItem() { int serviceId = 2; var testItem = new ServiceUpdateDto() { Shortname = "PHONE", Name = "Phone service" }; var response = controller.UpdateService(serviceId, testItem); Assert.IsType <NoContentResult>(response); var okResult = controller.GetServiceById(serviceId).Result as OkObjectResult; Assert.IsType <ServiceReadDto>(okResult.Value); Assert.Equal(serviceId, (okResult.Value as ServiceReadDto).Id); var item = okResult.Value as ServiceReadDto; Assert.Equal(serviceId, item.Id); Assert.IsType <ServiceReadDto>(item); Assert.Equal(testItem.Shortname, item.Shortname); Assert.Equal(testItem.Name, item.Name); }
public async Task <ActionResult> UpdateWorkshopService([FromRoute] int workshopId, [FromRoute] int serviceId, [FromBody] ServiceUpdateDto serviceUpdateDto) { if (!ModelState.IsValid) { return(BadRequest(serviceUpdateDto)); } Service service = await _serviceRepo.GetWorkshopSingleService(workshopId, serviceId); if (service == null) { return(NotFound(new { error = $"service with id {serviceId} could not be found in workshop with id {workshopId}" })); } else { //service = _mapper.Map<Service>(serviceUpdateDto); service.WorkshopID = workshopId; service.Name = serviceUpdateDto.Name; service.Price = serviceUpdateDto.Price; service.RepairTimeInHours = service.RepairTimeInHours; await _serviceRepo.UpdateAsync(service); return(NoContent()); } }