コード例 #1
0
ファイル: Repository.cs プロジェクト: will4u2/com.libera
        public async Task <IStandardReply <T> > DeleteAsync <T>(long id, long userId, long applicationId) where T : BaseClass
        {
            IStandardReply <T> reply = StandardReply <T> .CreateInstance();

            try
            {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Close();
                    conn.Open();
                }
                var deleteObject = conn.Get <T>(id);
                deleteObject.DeleteApplication = applicationId;
                deleteObject.DeleteDate        = DateTime.Now;
                deleteObject.DeleteUser        = userId;
                if (SoftDelete)
                {
                    await conn.UpdateAsync <T>(deleteObject);
                }
                else
                {
                    await conn.DeleteAsync <T>(deleteObject);
                }
                reply.Response = deleteObject;
                reply.Messages.Add($"Deletion for id: {id} was successful.");
            }
            catch (Exception exc)
            {
                reply.ProcessException(exc, new { id, userId, applicationId }, logger, MethodBase.GetCurrentMethod().Name, false);
            }
            return(reply);
        }
コード例 #2
0
        public async Task <IStandardReply <IEnumerable <Coin> > > AddCoinToTillAsync(Coin coin, int quantity, long userId, long applicationId)
        {
            IStandardReply <IEnumerable <Coin> > reply = StandardReply <IEnumerable <Coin> > .CreateInstance();

            coin.Quantity = quantity;
            try
            {
                var result = await AddCoinToTillAsync(coin, userId, applicationId);

                if (result.Success)
                {
                    reply = await GetTillAsync(userId, applicationId);
                }
                else
                {
                    reply.Messages.AddRange(result.Messages);
                    reply.Exceptions.AddRange(result.Exceptions);
                    reply.Success = false;
                }
            }
            catch (Exception exc)
            {
                reply.ProcessException(exc, coin, logger, MethodBase.GetCurrentMethod().Name, false);
            }
            return(reply);
        }
コード例 #3
0
ファイル: Repository.cs プロジェクト: will4u2/com.libera
        public async Task <IStandardReply <IEnumerable <T> > > GetEnumerableAsync <T>(Func <T, bool> filter, long userId, long applicationId) where T : BaseClass
        {
            IStandardReply <IEnumerable <T> > reply = StandardReply <IEnumerable <T> > .CreateInstance();

            try
            {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Close();
                    conn.Open();
                }
                if (filter != null)
                {
                    reply.Response = (await conn.GetAllAsync <T>()).Where(filter).ToList();
                }
                else
                {
                    reply.Response = (await conn.GetAllAsync <T>()).ToList();
                }
                reply.Messages.Add($"Retrival of type {typeof(T).Name} was successful.");
            }
            catch (Exception exc)
            {
                reply.ProcessException(exc, new { type = typeof(T).Name }, logger, MethodBase.GetCurrentMethod().Name, false);
            }
            return(reply);
        }
コード例 #4
0
        public async Task <IStandardReply <decimal> > GetTillCurrentAmount(long userId, long applicationId)
        {
            IStandardReply <decimal> reply = StandardReply <decimal> .CreateInstance();

            var till = await GetTillAsync(userId, applicationId);

            reply.Response = till.Response.Sum(t => t.Quantity * t.Type.Value);
            return(reply);
        }
コード例 #5
0
        public async Task <IStandardReply <IEnumerable <Coin> > > GetTillAsync(long userId, long applicationId)
        {
            IStandardReply <IEnumerable <Coin> > result = await repo.GetEnumerableAsync <Coin>(null, userId, applicationId);

            result.Response.ToList().ForEach(async(coin) => {
                coin.Type = (await coinTypeService.GetCoinTypeAsync(coin.CoinTypeId, userId, applicationId)).Response;
            });
            return(result);
        }
コード例 #6
0
        public async Task <IStandardReply <IEnumerable <Coin> > > AddCoinToTillAsync(long typeId, int quantity, long userId, long applicationId)
        {
            IStandardReply <IEnumerable <Coin> > reply = StandardReply <IEnumerable <Coin> > .CreateInstance();

            Coin coin = Coin.CreateInstance <Coin>(userId, applicationId);

            coin.Quantity   = quantity;
            coin.CoinTypeId = typeId;
            return(await AddCoinToTillAsync(coin, quantity, userId, applicationId));
        }
コード例 #7
0
        public async Task <IStandardReply <IEnumerable <Coin> > > AdjustTillAsync(IEnumerable <Coin> coins, long userId, long applicationId)
        {
            IStandardReply <IEnumerable <Coin> > reply = StandardReply <IEnumerable <Coin> > .CreateInstance();

            try
            {
                coins.ToList().ForEach(async(coin) => {
                    Coin lc      = (await repo.GetSingleAsync <Coin>(coin.ID, userId, applicationId)).Response;
                    lc.Quantity -= coin.Quantity;
                    await repo.SaveAsync <Coin>(lc, userId, applicationId);
                });
            }
            catch (Exception exc)
            {
                reply.ProcessException(exc, coins, logger, MethodBase.GetCurrentMethod().Name, false);
            }
            return(reply);
        }
コード例 #8
0
        public async Task <IStandardReply <IEnumerable <Coin> > > SwapTillAsync(IEnumerable <Coin> coins, long userId, long applicationId)
        {
            IStandardReply <IEnumerable <Coin> > reply = StandardReply <IEnumerable <Coin> > .CreateInstance();

            try
            {
                (await GetTillAsync(userId, applicationId)).Response.ToList().ForEach(async(coin) => {
                    var result = await DeleteAsync <Coin>(coin.ID, userId, applicationId);
                    reply.Messages.AddRange(result.Messages);
                    reply.Exceptions.AddRange(result.Exceptions);
                });
                reply = await FillTillAsync(coins, userId, applicationId);
            }
            catch (Exception exc)
            {
                reply.ProcessException(exc, coins, logger, MethodBase.GetCurrentMethod().Name, false);
            }
            return(reply);
        }
コード例 #9
0
ファイル: Repository.cs プロジェクト: will4u2/com.libera
        public async Task <IStandardReply <T> > GetSingleAsync <T>(Func <T, bool> filter, long userId, long applicationId) where T : BaseClass
        {
            IStandardReply <T> reply = StandardReply <T> .CreateInstance();

            try
            {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Close();
                    conn.Open();
                }
                reply.Response = (await conn.GetAllAsync <T>()).Where(filter).FirstOrDefault();
                reply.Messages.Add($"Retrival for id: {filter} was successful.");
            }
            catch (Exception exc)
            {
                reply.ProcessException(exc, new { filter, userId, applicationId }, logger, MethodBase.GetCurrentMethod().Name, false);
            }
            return(reply);
        }
コード例 #10
0
ファイル: Repository.cs プロジェクト: will4u2/com.libera
        public IStandardReply <T> GetSingle <T>(long id, long userId, long applicationId) where T : BaseClass
        {
            IStandardReply <T> reply = StandardReply <T> .CreateInstance();

            try
            {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Close();
                    conn.Open();
                }
                reply.Response = conn.Get <T>(id);
                reply.Messages.Add($"Retrival for id: {id} was successful.");
            }
            catch (Exception exc)
            {
                reply.ProcessException(exc, new { id, userId, applicationId }, logger, MethodBase.GetCurrentMethod().Name, false);
            }
            return(reply);
        }
コード例 #11
0
ファイル: Repository.cs プロジェクト: will4u2/com.libera
        public async Task <IStandardReply <IEnumerable <T> > > GetEnumerableAsync <T>(IRequestObject request)
        {
            logger.LogInformation("{0} started.", MethodBase.GetCurrentMethod().Name);
            IStandardReply <IEnumerable <T> > reply = StandardReply <IEnumerable <T> > .CreateInstance("Get successful.");

            try
            {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Close();
                    conn.Open();
                }
                reply.Response = (await conn.QueryAsync <T>(request.SqlStatement, request.Data, request.Transaction, request.Timeout, request.CommandType));
            }
            catch (Exception exc)
            {
                reply.ProcessException(exc, request, logger, MethodBase.GetCurrentMethod().Name);
            }
            logger.LogInformation("{0} finished.", MethodBase.GetCurrentMethod().Name);
            return(reply);
        }
コード例 #12
0
        public async Task <IStandardReply <IEnumerable <Coin> > > FillTillAsync(IEnumerable <Coin> coins, long userId, long applicationId)
        {
            IStandardReply <IEnumerable <Coin> > reply = StandardReply <IEnumerable <Coin> > .CreateInstance();

            try
            {
                coins.ToList().ForEach(async(coin) => {
                    var result = await AddCoinToTillAsync(coin, userId, applicationId);
                    if (!result.Success)
                    {
                        reply.Messages.AddRange(result.Messages);
                        reply.Exceptions.AddRange(result.Exceptions);
                        reply.Success = false;
                    }
                });
            }
            catch (Exception exc)
            {
                reply.ProcessException(exc, coins, logger, MethodBase.GetCurrentMethod().Name, false);
            }
            return(reply);
        }
コード例 #13
0
        public async Task <IStandardReply <IEnumerable <Coin> > > GetCorrectChangeAsync(decimal amountToChange, long userId, long applicationId)
        {
            IStandardReply <IEnumerable <Coin> > reply = StandardReply <IEnumerable <Coin> > .CreateInstance();

            try
            {
                List <Coin> change = new List <Coin>();
                while (amountToChange > 0)
                {
                    Coin currentCoin = await coinChecker(amountToChange, userId, applicationId);

                    change.Add(currentCoin);
                    amountToChange -= currentCoin.Quantity * currentCoin.Type.Value;
                }
                await AdjustTillAsync(change, userId, applicationId);

                reply.Response = change;
            }
            catch (Exception exc)
            {
                reply.ProcessException(exc, new { amountToChange }, logger, MethodBase.GetCurrentMethod().Name, false);
            }
            return(reply);
        }
コード例 #14
0
ファイル: Repository.cs プロジェクト: will4u2/com.libera
        public async Task <IStandardReply <T> > SaveAsync <T>(T data, long userId, long applicationId, Func <T, bool> dupeCheck = null) where T : BaseClass
        {
            data.InUser        = data.InUser == 0 ? userId : data.InUser;
            data.InApplication = data.InApplication == 0 ? applicationId : data.InApplication;
            data.InDate        = data.InUser == 0 ? DateTime.Now : data.InDate;

            IStandardReply <T> reply = StandardReply <T> .CreateInstance();

            try
            {
                if (conn.State != ConnectionState.Open)
                {
                    conn.Close();
                    conn.Open();
                }
                if (data.ID > 0)
                {
                    data.ModificationApplication = applicationId;
                    data.ModificationDate        = DateTime.Now;
                    data.ModificationUser        = userId;
                    var updateResult = await conn.UpdateAsync <T>(data);

                    if (!updateResult)
                    {
                        throw new Exception($"Update failed for {JsonConvert.SerializeObject(data)}");
                    }
                    reply.Response = data;
                }
                else
                {
                    if (dupeCheck != null)
                    {
                        var localData = conn.GetAll <T>().FirstOrDefault(dupeCheck);
                        if (localData != null)
                        {
                            data.InDate                  = localData.InDate;
                            data.InApplication           = localData.InApplication;
                            data.InUser                  = localData.InUser;
                            data.ModificationApplication = applicationId;
                            data.ModificationDate        = DateTime.Now;
                            data.ModificationUser        = userId;
                            data.ID = localData.ID;
                            var updateResult = await conn.UpdateAsync <T>(data);

                            reply.Response = data;
                            if (!updateResult)
                            {
                                throw new Exception($"Update failed for {JsonConvert.SerializeObject(data)}");
                            }
                        }
                        else
                        {
                            var insertResult = await conn.InsertAsync <T>(data);

                            data.ID        = insertResult;
                            reply.Response = data;
                            if (insertResult < 1)
                            {
                                throw new Exception($"Insert failed for {JsonConvert.SerializeObject(data)}");
                            }
                        }
                    }
                    else
                    {
                        var insertResult = await conn.InsertAsync <T>(data);

                        data.ID        = insertResult;
                        reply.Response = data;
                        if (insertResult < 1)
                        {
                            throw new Exception($"Insert failed for {JsonConvert.SerializeObject(data)}");
                        }
                    }
                }
                reply.Messages.Add($"Save for type: {typeof(T).Name} was successful.");
            }
            catch (Exception exc)
            {
                reply.ProcessException(exc, data, logger, MethodBase.GetCurrentMethod().Name, false);
            }
            return(reply);
        }