Beispiel #1
0
        public async Task <IActionResult> CreateTransaction([FromForm] Req_Transaction request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new Res_Common
                {
                    Success = false,
                    Errors = ModelState.Values.SelectMany(x => x.Errors.Select(xx => xx.ErrorMessage))
                }));
            }
            if (request.Amount == null)
            {
                request.Amount = 0;
            }

            var tranToCreate = _mapper.Map <Transaction>(request);

            if (tranToCreate.DtTransaction is null)
            {
                tranToCreate.DtTransaction = DateTime.Now;
            }

            List <IFormFile> imagesToCreate = new List <IFormFile>();

            PropertyInfo[] props = request.GetType().GetProperties();
            foreach (var p in props.Where(x => x.PropertyType == typeof(IFormFile) && x.GetValue(request, null) != null))
            {
                IFormFile f = (IFormFile)p.GetValue(request, null);
                if (f != null)
                {
                    imagesToCreate.Add(f);
                }
            }



            IList <string> Errors = await _transactionService.CreateTransactionAsync(tranToCreate, imagesToCreate, request.ByPassSkuValidation);

            var response = _mapper.Map <Res_Transaction>(tranToCreate);

            if (Errors.Count > 0)
            {
                response.Success = false;
                response.Errors  = Errors;
                return(BadRequest(response));
            }

            var baseUrl     = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}";
            var locationUri = baseUrl + "/" + ApiRoutes.Transaction.GetTransaction.Replace("{UniqueId}", tranToCreate.UniqueId.ToString());

            response.Success = true;
            return(Created(locationUri, response));
        }
Beispiel #2
0
        public async Task <IActionResult> UpdateTransaction([FromRoute] Guid UniqueId, [FromForm] Req_Transaction request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new Res_Common
                {
                    Success = false,
                    Errors = ModelState.Values.SelectMany(x => x.Errors.Select(xx => xx.ErrorMessage))
                }));
            }

            var tranOriginal = await _transactionService.GetTransactionByUniqueIdAsync(UniqueId);

            if (tranOriginal is null)
            {
                return(NotFound());
            }

            var auth  = Request.Headers["Authorization"];
            var token = auth.First().Remove(0, "Bearer ".Length).Trim();

            List <IFormFile> imagesToCreate = new List <IFormFile>();

            PropertyInfo[] props = request.GetType().GetProperties();
            foreach (var p in props.Where(x => x.PropertyType == typeof(IFormFile) && x.GetValue(request, null) != null))
            {
                IFormFile f = (IFormFile)p.GetValue(request, null);
                if (f != null)
                {
                    imagesToCreate.Add(f);
                }
            }

            var tranUpdate = _mapper.Map <Transaction>(tranOriginal);

            _mapper.Map <Req_Transaction, Transaction>(request, tranUpdate);
            IList <string> Errors = await _transactionService.UpdateTransactionAsync(tranOriginal, tranUpdate, imagesToCreate, token);

            var response = _mapper.Map <Res_Transaction>(tranUpdate);

            if (Errors.Count > 0)
            {
                response.Success = false;
                response.Errors  = Errors;
                return(BadRequest(response));
            }

            var baseUrl     = $"{HttpContext.Request.Scheme}://{HttpContext.Request.Host.ToUriComponent()}";
            var locationUri = baseUrl + "/" + ApiRoutes.Transaction.GetTransaction.Replace("{UniqueId}", tranUpdate.UniqueId.ToString());

            response.Success = true;
            return(Created(locationUri, response));
        }