public async Task <IActionResult> CreatePartAsync(CreatePartRequest request) { var mappedPart = Mapper.Map <CreatePartRequest, Part>(request); mappedPart.Keywords = request.Keywords?.Split(new string[] { " ", "," }, StringSplitOptions.RemoveEmptyEntries); var partType = await GetPartTypeAsync(request.PartTypeId); if (partType == null) { return(BadRequest($"Invalid Part Type: {request.PartTypeId}")); } mappedPart.PartTypeId = partType.PartTypeId; mappedPart.MountingTypeId = GetMountingTypeId(request.MountingTypeId); if (mappedPart.MountingTypeId <= 0) { return(BadRequest($"Invalid Mounting Type: {request.MountingTypeId}")); } var duplicatePartResponse = await CheckForDuplicateAsync(request, mappedPart); if (duplicatePartResponse != null) { return(duplicatePartResponse); } var part = await _partService.AddPartAsync(mappedPart); var partResponse = Mapper.Map <Part, PartResponse>(part); partResponse.PartType = partType.Name; partResponse.Keywords = string.Join(" ", part.Keywords ?? new List <string>()); return(Ok(partResponse)); }
public async Task <IActionResult> CreatePartAsync(CreatePartRequest request) { var partType = await _partService.GetOrCreatePartTypeAsync(new PartType { Name = request.PartType }); var mappedPart = Mapper.Map <CreatePartRequest, Part>(request); mappedPart.Keywords = request.Keywords?.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); mappedPart.PartTypeId = partType.PartTypeId; mappedPart.MountingTypeId = (int)Enum.Parse <MountingType>(request.MountingType.Replace(" ", ""), true); if (request is IPreventDuplicateResource && !((IPreventDuplicateResource)request).AllowPotentialDuplicate) { var existingSearch = await _partService.FindPartsAsync(mappedPart.PartNumber); if (existingSearch.Any()) { var existingParts = existingSearch.Select(x => x.Result).ToList(); var partsResponse = Mapper.Map <ICollection <Part>, ICollection <PartResponse> >(existingParts); var partTypes = await _partService.GetPartTypesAsync(); foreach (var p in partsResponse) { p.PartType = partTypes.Where(x => x.PartTypeId == p.PartTypeId).Select(x => x.Name).FirstOrDefault(); p.MountingType = ((MountingType)p.MountingTypeId).ToString(); } return(StatusCode((int)HttpStatusCode.Conflict, new PossibleDuplicateResponse(partsResponse))); } } var part = await _partService.AddPartAsync(mappedPart); var partResponse = Mapper.Map <Part, PartResponse>(part); partResponse.PartType = partType.Name; return(Ok(partResponse)); }
public async Task <IActionResult> AddPart([FromBody] AddPartRequest request, CancellationToken cancellationToken) { var result = await _partService.AddPartAsync(request, cancellationToken); return(Ok(result)); }