public async Task <IActionResult> Create([FromBody] InspectorDTO entity) { try { if (entity == null) { _logger.LogError("Inspector object sent from client is null."); return(BadRequest("Inspector object is null")); } if (!ModelState.IsValid) { _logger.LogError("Invalid inspector object sent from client."); return(BadRequest("Invalid model object")); } Inspector inspector = _mapper.Map <InspectorDTO, Inspector>(entity); _unitOfWork.Inspector.Create(inspector); await _unitOfWork.CompleteAsync(); InspectorDTO inspectorDTO = _mapper.Map <Inspector, InspectorDTO>(inspector); return(CreatedAtRoute("InspectorById", new { id = inspectorDTO.Id }, inspectorDTO)); } catch (Exception ex) { _logger.LogError($"Something went wrong inside Create action: {ex.Message}"); return(StatusCode(500, "Internal server error")); } }
public async Task <IActionResult> FindAsync(Guid id) { try { Inspector result = await _unitOfWork.Inspector.FindAsync(id); if (result == null) { _logger.LogError($"Inspector with id: {id}, hasn't been found in db."); return(NotFound()); } else { _logger.LogInformation($"Returned inspector with id: {id}"); InspectorDTO resultDTO = _mapper.Map <Inspector, InspectorDTO>(result); return(Ok(resultDTO)); } } catch (Exception ex) { _logger.LogError($"Something went wrong inside FindAsync action: {ex.Message}"); return(StatusCode(500, "Internal server error")); } }
public void TestDuplicateInspectionInspectorLogic() { using (var context = InitAndGetDbContext()) { //Arrange var repositori = new InspectionRepository(context); Inspector Inspector1 = new Inspector() { Id = Guid.NewGuid(), Name = "Inspector 1", Created = DateTime.Today }; Inspection Inspection1 = new Inspection() { Id = Guid.NewGuid(), Customer = "Customer 1", Address = "Address 1", Observations = "Observation 1", Status = Status.Done, Created = DateTime.Today }; InspectionInspector relation1 = new InspectionInspector() { InspectionDate = DateTime.Today.AddDays(-1), InspectionId = Inspection1.Id, InspectorId = Inspector1.Id }; Inspection1.InspectionInspector = new List <InspectionInspector>() { relation1 }; repositori.Create(Inspection1); context.SaveChanges(); InspectorDTO inspectorDTO = new InspectorDTO() { Id = Inspector1.Id, Name = "Inspector 1" }; InspectionDTO InspectionDTO = new InspectionDTO() { Id = Inspection1.Id, Customer = "Customer 1", Address = "Address 1", Observations = "Observation 1", status = Status.Done, InspectionDate = relation1.InspectionDate, Inspectors = new List <InspectorDTO>() { inspectorDTO } }; //Act Inspection found = repositori.GetSingleEagerAsync(o => o.Id == InspectionDTO.Id && o.InspectionInspector.Any(ii => ii.InspectionDate == InspectionDTO.InspectionDate) && o.InspectionInspector.Any(ii => InspectionDTO.Inspectors.Any(i => i.Id == ii.InspectorId))).Result; //Assert Assert.NotNull(found); } }
public async Task <IActionResult> Create(IFormCollection collection) { try { var inspector = new InspectorDTO { Firstname = collection["Firstname"], LastName = collection["LastName"], MiddleName = collection["MiddleName"], Position = collection["Position"], Salary = Convert.ToDouble(collection["Salary"]) }; await _inspectorService.Create(inspector); _logger.LogInformation("Creation was successful."); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { _logger.LogError("Creation failed.", ex); return(View()); } }
public async Task <IActionResult> Update(Guid id, [FromBody] InspectorDTO entity) { try { if (entity == null) { _logger.LogError("Inspector object sent from client is null."); return(BadRequest("Inspector object is null")); } if (!ModelState.IsValid) { _logger.LogError("Invalid inspector object sent from client."); return(BadRequest("Invalid model object")); } Inspector inspector = _mapper.Map <InspectorDTO, Inspector>(entity); Inspector foundInspector = await _unitOfWork.Inspector.FindAsync(inspector.Id); if (foundInspector == null) { _logger.LogError($"Inspector with id: {id}, hasn't been found in db."); return(NotFound()); } foundInspector.Update(inspector); await _unitOfWork.CompleteAsync(); return(NoContent()); } catch (Exception ex) { _logger.LogError($"Something went wrong inside Update action: {ex.Message}"); return(StatusCode(500, "Internal server error")); } }