Exemplo n.º 1
0
    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));
        }
    }
Exemplo n.º 2
0
        /// <summary>
        /// Get the holding with the specified identifier.
        /// </summary>
        /// <returns>The holding.</returns>
        /// <param name="id">Identifier.</param>
        public HoldingEntity Get(string id)
        {
            List <HoldingEntity> holdingEntities = xmlDatabase.LoadEntities().ToList();
            HoldingEntity        holdingEntity   = holdingEntities.FirstOrDefault(x => x.Id == id);

            if (holdingEntity == null)
            {
                throw new EntityNotFoundException(id, nameof(BorderEntity).Replace("Entity", ""));
            }

            return(holdingEntity);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Converts the domain model into an entity.
        /// </summary>
        /// <returns>The entity.</returns>
        /// <param name="holding">Holding.</param>
        internal static HoldingEntity ToEntity(this Holding holding)
        {
            HoldingEntity holdingEntity = new HoldingEntity
            {
                Id          = holding.Id,
                Name        = holding.Name,
                Description = holding.Description,
                Type        = holding.Type.ToString(),
                ProvinceId  = holding.ProvinceId
            };

            return(holdingEntity);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Converts the entity into a domain model.
        /// </summary>
        /// <returns>The domain model.</returns>
        /// <param name="holdingEntity">Holding entity.</param>
        internal static Holding ToDomainModel(this HoldingEntity holdingEntity)
        {
            Holding holding = new Holding
            {
                Id          = holdingEntity.Id,
                Name        = holdingEntity.Name,
                Description = holdingEntity.Description,
                Type        = (HoldingType)Enum.Parse(typeof(HoldingType), holdingEntity.Type),
                ProvinceId  = holdingEntity.ProvinceId
            };

            return(holding);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Adds the specified holding.
        /// </summary>
        /// <param name="holdingEntity">Holding.</param>
        public void Add(HoldingEntity holdingEntity)
        {
            List <HoldingEntity> holdingEntities = xmlDatabase.LoadEntities().ToList();

            holdingEntities.Add(holdingEntity);

            try
            {
                xmlDatabase.SaveEntities(holdingEntities);
            }
            catch
            {
                throw new DuplicateEntityException(holdingEntity.Id, nameof(HoldingEntity).Replace("Entity", ""));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Updates the specified holding.
        /// </summary>
        /// <param name="holdingEntity">Holding.</param>
        public void Update(HoldingEntity holdingEntity)
        {
            List <HoldingEntity> holdingEntities       = xmlDatabase.LoadEntities().ToList();
            HoldingEntity        holdingEntityToUpdate = holdingEntities.FirstOrDefault(x => x.Id == holdingEntity.Id);

            if (holdingEntityToUpdate == null)
            {
                throw new EntityNotFoundException(holdingEntity.Id, nameof(BorderEntity).Replace("Entity", ""));
            }

            holdingEntityToUpdate.Name        = holdingEntity.Name;
            holdingEntityToUpdate.Description = holdingEntity.Description;
            holdingEntityToUpdate.Type        = holdingEntity.Type;

            xmlDatabase.SaveEntities(holdingEntities);
        }
Exemplo n.º 7
0
    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));
        }
    }
Exemplo n.º 8
0
 public static HoldingModel?FromEntity(HoldingEntity entity) => entity is null ? null : new()
Exemplo n.º 9
0
    public async Task <DalResult> BuyFromExchangeAsync(int userid, int beanid, long quantity)
    {
        if (userid <= 0)
        {
            return(new(DalErrorCode.Invalid, new("User id is invalid")));
        }
        if (beanid <= 0)
        {
            return(new(DalErrorCode.Invalid, new("Bean id is invalid")));
        }
        using var conn = new SqlConnection(ConnectionString);
        await conn.OpenAsync();

        using var transaction = await conn.BeginTransactionAsync();

        try
        {
            var sql  = $"select * from Users where Id={userid};";
            var user = await conn.QueryFirstOrDefaultAsync <UserEntity>(sql, transaction : transaction);

            if (user is null)
            {
                await transaction.RollbackAsync();

                return(new(DalErrorCode.NotFound, new($"No user with the id '{userid}' was found")));
            }
            sql = $"select * from Beans where Id={beanid};";
            var bean = await conn.QueryFirstOrDefaultAsync <BeanEntity>(sql, transaction : transaction);

            if (bean is null)
            {
                await transaction.RollbackAsync();

                return(new(DalErrorCode.NotFound, new($"No bean with the id '{beanid}' was found")));
            }
            if (quantity > bean.Outstanding)
            {
                await transaction.RollbackAsync();

                return(new(DalErrorCode.NSF, new("Insufficient outstanding beans for that quantity")));
            }
            if (quantity * bean.Price > user.Balance)
            {
                await transaction.RollbackAsync();

                return(new(DalErrorCode.NSF, new("User has insufficient funds to buy that many beans")));
            }
            user.Balance     -= quantity * bean.Price;
            bean.Outstanding -= quantity;
            var holding = new HoldingEntity
            {
                Id           = 0,
                UserId       = userid,
                BeanId       = beanid,
                PurchaseDate = DateTime.UtcNow,
                Price        = bean.Price,
                Quantity     = quantity,
                Bean         = null
            };
            await conn.InsertAsync(holding, transaction : transaction);

            await conn.UpdateAsync(user, transaction : transaction);

            await conn.UpdateAsync(bean, transaction : transaction);

            await transaction.CommitAsync();

            return(DalResult.Success);
        }
        catch (Exception ex)
        {
            await transaction.RollbackAsync();

            return(DalResult.FromException(ex));
        }
        finally
        {
            await conn.CloseAsync();
        }
    }