public static Limit ToDomain(this LimitDto code, Limit originalCode = null) { if (originalCode != null && originalCode.Id == code.Id) { originalCode.LimitTypeId = code.LimitTypeId; originalCode.Amount = code.Amount; originalCode.Percentage = code.Percentage; originalCode.Number = code.Number; originalCode.Description = code.Description; ((IDataDto)code).ToDomain((IData)originalCode); return(originalCode); } var data = new Limit { LimitTypeId = code.LimitTypeId, Amount = code.Amount, Percentage = code.Percentage, Number = code.Number, Description = code.Description }; ((IDataDto)code).ToDomain((IData)data); return(data); }
// POST: api/Product public IHttpActionResult Post([FromBody] LimitDto value) { try { var originalEntity = _service.Gets(value.Id).FirstOrDefault(); var entity = value.ToDomain(originalEntity); _service.Add(entity); return(Ok(entity.Id)); } catch (Exception ex) { return(InternalServerError()); } }
public static LimitDto ToDto(this Limit code) { var dto = new LimitDto { LimitTypeId = code.LimitTypeId, Amount = code.Amount, Percentage = code.Percentage, Number = code.Number, Description = code.Description }; ((IData)code).ToDto((IDataDto)dto); return(dto); }
public void Post() { try { using (var server = TestServer.Create <MyStartup>()) { var dto = new LimitDto { Id = 0, Amount = 50, LimitTypeId = 1 }; HttpResponseMessage response; response = server.HttpClient.PostAsJsonAsync("api/Limit", dto).Result; } } catch (Exception ex) { } }
public LimitDto UpdateLimit(UpdateLimitInput input) { DateTime oldestDate = new DateTime(2014, 1, 1); // This method accepts one limit for one variable. // If the limit does not exist, it is added. // If the limit exists, it is changed or deleted. LimitDto output = null; bool inputIsValid = false; bool isActive = input.IsActive.HasValue ? input.IsActive.Value : true; // Look for the variable. IOWVariable variable = _iowManager.FirstOrDefaultVariable(input.IOWVariableId, input.VariableName); if( variable != null ) { // Does the specified limit exist? If so, get it. This will return null if nothing is found. IOWLimit limit = _iowManager.FirstOrDefaultLimit(variable.Id, input.Id, input.LevelName); // Does the specified level exist? It should. IOWLevel level = _iowManager.FirstOrDefaultLevel(input.IOWLevelId, input.LevelName); // There are five possibilities at this point: // 1) We did not find a level ==> bad input, do nothing // 2) We found a level, found a limit, isActive flag is true ==> update an existing limit // 3) We found a level, found a limit, IsActive flag is false ==> delete an existing limit // 4) We found a level, did not find a limit, IsActive flag is true ==> insert a new limit // 5) We found a level, did not find a limit, IsActive flag is false ==> do nothing if ( level == null ) { // Case 1: Bad input } else if ( isActive ) { // Case 2: IsActive is true AND limit exists ==> update an existing limit // Case 4: IsActive is true AND limit does not exist ==> insert a new limit // For case 4 (limit does not exist), need to create a limit record to continue if ( limit == null ) limit = new IOWLimit { IOWVariableId = variable.Id, IOWLevelId = level.Id, LastCheckDate = null, LastStatus = IOWStatus.Normal, LastDeviationStartTimestamp = null, LastDeviationEndTimestamp = null, TenantId = variable.TenantId }; limit.StartDate = new DateTime(Math.Max(input.StartDate.Ticks, oldestDate.Ticks)); limit.EndDate = input.EndDate; limit.Direction = input.Direction; limit.Value = input.Value; limit.Cause = input.Cause; limit.Consequences = input.Consequences; limit.Action = input.Action; limit.Id = _iowManager.InsertOrUpdateLimitAndGetId(limit); inputIsValid = true; } else if ( limit != null && !isActive ) { // Case 3: Limit exists and should be deleted _iowManager.DeleteLimit(limit.Id); inputIsValid = true; } if( inputIsValid ) output = new LimitDto { Id = limit.Id, IOWVariableId = variable.Id, IsActive = (limit != null && isActive ) ? true : false, IOWLevelId = limit.IOWLevelId, Name = level.Name, Criticality = level.Criticality, StartDate = limit.StartDate, EndDate = limit.EndDate, Direction = limit.Direction, Value = limit.Value, Cause = limit.Cause, Consequences = limit.Consequences, Action = limit.Action }; } return output; }