public async Task <ApiError> InsertAsync(HoldingModel model) { var checkresult = await ValidateModelAsync(model); if (!checkresult.Successful) { return(checkresult); } HoldingEntity entity = model !; try { var result = await _holdingRepository.InsertAsync(entity); if (result.Successful) { model.Id = entity.Id; } return(ApiError.FromDalResult(result)); } catch (Exception ex) { return(ApiError.FromException(ex)); } }
private async Task <ApiError> ValidateModelAsync(HoldingModel model, bool checkid = false) { if (model is null || model.UserId <= 0 || model.BeanId <= 0 || model.Quantity == 0 || model.Price <= 0M) { return(new(Strings.InvalidModel)); } if (model.PurchaseDate == default) { model.PurchaseDate = DateTime.UtcNow; } var user = await _userRepository.ReadAsync(model.UserId); if (user is null) { return(new(string.Format(Strings.NotFound, "user", "id", model.UserId))); } var bean = await _beanRepository.ReadAsync(model.BeanId); if (bean is null) { return(new(string.Format(Strings.NotFound, "bean", "id", model.BeanId))); } if (model.Id < 0) { model.Id = 0; } if (checkid && model.Id == 0) { return(new(string.Format(Strings.Invalid, "id"))); } return(ApiError.Success); }
public async Task <HoldingModel?> ReadAsync(int id) { var entity = await _holdingRepository.ReadAsync(id); if (entity is not null) { HoldingModel model = entity !; model.CanDelete = true; return(model); } return(null); }
public async Task <ApiError> DeleteAsync(HoldingModel model) { if (model is null) { return(new(Strings.InvalidModel)); } try { return(ApiError.FromDalResult(await _holdingRepository.DeleteAsync(model.Id))); } catch (Exception ex) { return(ApiError.FromException(ex)); } }
public async Task <ApiError> UpdateAsync(HoldingModel model) { var checkresult = await ValidateModelAsync(model, true); if (!checkresult.Successful) { return(checkresult); } HoldingEntity entity = model !; try { return(ApiError.FromDalResult(await _holdingRepository.UpdateAsync(entity))); } catch (Exception ex) { return(ApiError.FromException(ex)); } }
public HoldingViewModel(HoldingModel holding) { HoldingModel = holding; }