public GatewayDetailDto Add(CreateGatewayInput input) { var entity = Mapper.Map <CreateGatewayInput, Gateway>(input); // IPv4 Validation var isValidIPAddress = IPAddress.TryParse(input.IPv4, out var ipAddress); if (!isValidIPAddress) { return new GatewayDetailDto { StatusCode = HttpStatusCode.BadRequest, StatusMessage = "You must provide a valid IPv4. Ex: 192.168.1.1", } } ; try { using (_uowFactory.Create()) { _gatewayRepo.Add(entity); } } catch (Exception e) { var dtoWithErrors = Mapper.Map <Gateway, GatewayDetailDto>(entity); dtoWithErrors.StatusCode = HttpStatusCode.BadRequest; var msg = new StringBuilder(); msg.AppendLine(e.Message); msg.AppendLine(e.InnerException?.Message); msg.AppendLine(e.InnerException?.InnerException?.Message); dtoWithErrors.StatusMessage = $"Error occurred when processing request: {msg}"; return(dtoWithErrors); } var dto = Mapper.Map <Gateway, GatewayDetailDto>(entity); dto.StatusCode = HttpStatusCode.Created; dto.StatusMessage = "Gateway created successfully."; return(dto); }
public IHttpActionResult Add(int networkId, GatewayModelPost gatewayModel) { _credentials.SetCredentials(Request.Headers.Authorization.Parameter); var userId = _userRep.Get(_credentials.Email, _credentials.Password).Id; if (!_networkRep.GetAll().Any(n => n.Id == networkId && n.User_Id == userId)) { var errorMessage = _messages.GetMessage(Custom.NotFound, "Network", "Id"); return(NotFound(errorMessage)); } if (gatewayModel == null) { var errorMessage = _messages.GetMessage(Generic.NullObject); return(BadRequest(errorMessage)); } if (_gatewayRep.GetAll().Any(g => g.Name == gatewayModel.Name)) { var errorMessage = _messages.GetMessage(Custom.Conflict, "Gateway", "Name"); return(Conflict(errorMessage)); } if (_gatewayRep.GetAll().Any(g => g.Address == gatewayModel.Address)) { var errorMessage = _messages.GetMessage(Custom.Conflict, "Gateway", "Address"); return(Conflict(errorMessage)); } var newGateway = _mapper.Map <Gateway>(gatewayModel); newGateway.Network_Id = networkId; newGateway.ProductionDate = _dateTime.GetDateTime(); _gatewayRep.Add(newGateway); var createdGateway = _mapper.Map <GatewayModelGet>(newGateway); return(CreatedAtRoute("GetGateway", new { networkId, id = createdGateway.Id }, createdGateway)); }
public void AddGatewayAsync(Gateway model) { gatewayRepository.Add(mapper.Map <GatewayEntity>(model)); }