Ejemplo n.º 1
0
    public async Task <ApiError> InsertAsync(BeanModel model)
    {
        var checkresult = await ValidateModelAsync(model);

        if (!checkresult.Successful)
        {
            return(checkresult);
        }
        BeanEntity entity = model !;

        try
        {
            var result = await _beanRepository.InsertAsync(entity);

            if (result.Successful)
            {
                model.Id = entity.Id;
            }
            return(ApiError.FromDalResult(result));
        }
        catch (Exception ex)
        {
            return(ApiError.FromException(ex));
        }
    }
Ejemplo n.º 2
0
    private async Task <ApiError> ValidateModelAsync(BeanModel model, bool checkid = false, bool update = false)
    {
        if (model is null || string.IsNullOrWhiteSpace(model.Name) || model.Price <= 0M || string.IsNullOrWhiteSpace(model.Filename))
        {
            return(new(Strings.InvalidModel));
        }
        if (model.Id < 0)
        {
            model.Id = 0;
        }
        if (checkid && model.Id == 0)
        {
            return(new(string.Format(Strings.Invalid, "id")));
        }
        var existing = await _beanRepository.ReadAsync(model.Name);

        if (update)
        {
            if (existing is not null && existing.Id != model.Id)
            {
                return(new(string.Format(Strings.Duplicate, "a", "bean", "name", model.Name)));
            }
        }
        else if (existing is not null)
        {
            return(new(string.Format(Strings.Duplicate, "a", "bean", "name", model.Name)));
        }
        if (model.ARGB == 0)
        {
            model.ARGB = (long)_colorService.GetLongARGB(model.Name);
        }
        return(ApiError.Success);
    }
Ejemplo n.º 3
0
 public async Task <ApiError> DeleteAsync(BeanModel model)
 {
     if (model is null)
     {
         return(new(Strings.InvalidModel));
     }
     try
     {
         return(ApiError.FromDalResult(await _beanRepository.DeleteAsync(model.Id)));
     }
     catch (Exception ex)
     {
         return(ApiError.FromException(ex));
     }
 }
Ejemplo n.º 4
0
    public async Task <ApiError> UpdateAsync(BeanModel model)
    {
        var checkresult = await ValidateModelAsync(model, true, true);

        if (!checkresult.Successful)
        {
            return(checkresult);
        }
        BeanEntity entity = model !;

        try
        {
            return(ApiError.FromDalResult(await _beanRepository.UpdateAsync(entity)));
        }
        catch (Exception ex)
        {
            return(ApiError.FromException(ex));
        }
    }
Ejemplo n.º 5
0
 public HoldingDisplayModel(BeanModel model, IEnumerable <HoldingModel> holdings) : this()
 {
     if (model is null)
     {
         throw new ArgumentNullException(nameof(model));
     }
     BeanId   = model.Id;
     BeanName = model.Name;
     if (holdings is not null && holdings.Any())
     {
         HoldingItems = holdings.Count();
         Quantity     = holdings.Sum(x => x.Quantity);
         var basis = 0M;
         foreach (var holding in holdings)
         {
             basis += holding.Quantity * holding.Price;
         }
         Basis = basis / Quantity;
     }
 }
Ejemplo n.º 6
0
    public async Task <ApiError> InsertAsync(MovementModel movement, BeanModel bean)
    {
        var checkresult = await ValidateModelAsync(movement);

        if (!checkresult.Successful)
        {
            return(checkresult);
        }
        if (bean is null)
        {
            return(new(string.Format(Strings.Invalid, "bean model")));
        }
        MovementEntity movementEntity = movement !;
        BeanEntity     beanEntity     = bean !;

        try
        {
            return(ApiError.FromDalResult(await _movementRepository.InsertAsync(movementEntity, beanEntity)));
        }
        catch (Exception ex)
        {
            return(ApiError.FromException(ex));
        }
    }