예제 #1
0
        public async Task <IActionResult> AddNewExpense([FromBody] ExpensesRequest expensesRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            string receiptPath = string.Empty;

            if (expensesRequest.PictureArray != null && expensesRequest.PictureArray.Length > 0)
            {
                receiptPath = _imageHelper.UploadImage(expensesRequest.PictureArray, "Receipts");
            }

            TripDetailsEntity tripDetails = await _context.TripDetails
                                            .Include(t => t.Expenses)
                                            .FirstOrDefaultAsync(t => t.Id == expensesRequest.Id);

            if (tripDetails == null)
            {
                return(BadRequest("Trip not found."));
            }

            if (tripDetails.Expenses == null)
            {
                tripDetails.Expenses = new List <ExpensesEntity>();
            }


            tripDetails.Expenses.Add(new ExpensesEntity
            {
                Value       = expensesRequest.Value,
                ExpenseType = expensesRequest.ExpensesType,
                //CreatedDate = DateTime.UtcNow
                BillPath = receiptPath
            });


            _context.TripDetails.Update(tripDetails);
            await _context.SaveChangesAsync();

            return(NoContent());
        }
예제 #2
0
        public async Task <IActionResult> AddDetails([FromBody] AddDetailsRequest modelRequest)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState
                                  ));
            }

            CultureInfo cultureInfo = new CultureInfo(modelRequest.CultureInfo);

            Resource.Culture = cultureInfo;

            TripsEntity trip = await _dataContext.Trips.FindAsync(int.Parse(modelRequest.TripId));

            if (trip == null)
            {
                return(BadRequest(Resource.TripIdIncorrect
                                  ));
            }

            string picturePath = string.Empty;

            if (modelRequest.VoucherPath != null && modelRequest.VoucherPath.Length > 0)
            {
                picturePath = _imageHelper.UploadImage(modelRequest.VoucherPath, "Vouchers");
            }

            TripDetailsEntity tripDetail = new TripDetailsEntity()
            {
                ExpensesType = await _dataContext.ExpensesTypes.FindAsync(int.Parse(modelRequest.ExpensesTypeId)),
                Date         = modelRequest.Date,
                Cost         = modelRequest.Cost,
                VoucherPath  = picturePath,
                Trip         = trip
            };

            _dataContext.TripDetails.Add(tripDetail);

            await _dataContext.SaveChangesAsync();

            return(Ok(Resource.DetailAdded));
        }