public static Model.TroubleCreationInfo Convert(Client.TroubleCreationInfo creationInfo, IReadOnlyList <Models.Tags.Tag> modelTags) { if (creationInfo == null) { throw new ArgumentNullException(nameof(creationInfo)); } if (modelTags == null) { throw new ArgumentNullException(nameof(modelTags)); } var filteredTagIds = TroubleConverterUtils.FilterWrongTagIds(creationInfo.Tags, modelTags).ToArray(); if (filteredTagIds == null || !filteredTagIds.Any()) { throw new InvalidDataException($"{nameof(filteredTagIds)} can't be empty."); } var coordinates = creationInfo.Coordinates.ToArray(); if (coordinates.Length != CoordinatesLength) { throw new InvalidDataException(nameof(creationInfo.Coordinates)); } var modelCreationInfo = new Model.TroubleCreationInfo(creationInfo.Name, creationInfo.Description, coordinates[0], coordinates[1], creationInfo.Address, filteredTagIds); return(modelCreationInfo); }
public async Task <IActionResult> CreateTroubleAsync([FromBody] Client.TroubleCreationInfo creationInfo, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (creationInfo == null) { var error = Responses.BodyIsMissing(nameof(creationInfo)); return(BadRequest(error)); } var author = HttpContext.User.Identity.Name; if (author == null) { var error = Responses.Unauthorized(nameof(author)); return(Unauthorized(error)); } Model.Trouble modelTrouble; try { var modelTags = await tagRepository.GetAllAsync(cancellationToken).ConfigureAwait(false); var modelCreationInfo = Converter.TroubleCreationInfoConverter.Convert(creationInfo, modelTags); modelTrouble = await troubleRepository.CreateAsync(modelCreationInfo, author, cancellationToken).ConfigureAwait(false); } catch (Exception ex) { if (ex is TroubleDuplicationException) { var error = Responses.DuplicationError(ex.Message, Target); return(BadRequest(error)); } else //if (ex is InvalidDataException) { var error = Responses.InvalidData(ex.Message, Target); return(BadRequest(error)); } } var clientTrouble = Converter.TroubleConverter.Convert(modelTrouble); return(CreatedAtRoute("GetTroubleRoute", new { id = clientTrouble.Id }, clientTrouble)); }