public TripDetailEntity tripDetailRequestToEntity(TripDetailRequest tripDetailRequest) { return(new TripDetailEntity { Name = tripDetailRequest.Name, Date = tripDetailRequest.Date, Expense = tripDetailRequest.Expense, Currency = tripDetailRequest.Currency, Comment = tripDetailRequest.Comment }); }
public async Task <IActionResult> PostTripDetailEntity([FromBody] TripDetailRequest tripDetailRequest) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } string voucherPath = string.Empty; if (tripDetailRequest.VoucherArray != null && tripDetailRequest.VoucherArray.Length > 0) { voucherPath = _imageHelper.UploadImage(tripDetailRequest.VoucherArray, "Vouchers"); } TripDetailEntity tripDetailEntity = _converterHelper.tripDetailRequestToEntity(tripDetailRequest); tripDetailEntity.Date = new DateTime(tripDetailRequest.Year, tripDetailRequest.Mounth, tripDetailRequest.Day, 0, 0, 0); tripDetailEntity.AttachmentPath = voucherPath; int id = int.Parse(tripDetailRequest.TripId); var tripEntity = await _context.Trips.FindAsync(id); if (tripEntity == null) { return(NotFound()); } else { tripDetailEntity.Trip = tripEntity; } id = int.Parse(tripDetailRequest.ExpenseTypeId); var expenseType = await _context.ExpenseTypes.FindAsync(id); if (expenseType == null) { return(NotFound()); } else { tripDetailEntity.ExpenseType = expenseType; } _context.TripDetails.Add(tripDetailEntity); var newTrip = await _context.SaveChangesAsync(); Response response = new Response { IsSuccess = true, Message = "Success", Result = tripDetailEntity }; return(Ok()); }
public async Task <Response> RegisterTripDetailAsync( string urlBase, string servicePrefix, string controller, string tokenType, string accessToken, TripDetailRequest tripDetailRequest) { try { string request = JsonConvert.SerializeObject(tripDetailRequest); StringContent content = new StringContent(request, Encoding.UTF8, "application/json"); HttpClient client = new HttpClient { BaseAddress = new Uri(urlBase) }; client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(tokenType, accessToken); string url = $"{servicePrefix}{controller}"; HttpResponseMessage response = await client.PostAsync(url, content); string answer = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { return(new Response { IsSuccess = false, Message = answer }); } Response obj = JsonConvert.DeserializeObject <Response>(answer); return(obj); } catch (Exception ex) { return(new Response { IsSuccess = false, Message = ex.Message }); } }
public async Task <IActionResult> PostTravelDetails([FromBody] TripDetailRequest TripDetailRequest) { if (!ModelState.IsValid) { return(BadRequest(new Response { IsSuccess = false, Message = "Bad request", Result = ModelState })); } CultureInfo cultureInfo = new CultureInfo(TripDetailRequest.CultureInfo); Resource.Culture = cultureInfo; var TripEntity = await _context.Trips.FindAsync(TripDetailRequest.TripId); if (TripEntity == null) { return(BadRequest(new Response { IsSuccess = false, Message = Resource.ThisTripDoesNotExist })); } var typeExpenseEntity = await _context.TripExpenseTypes.FindAsync(TripDetailRequest.TripExpenseTypeId); if (typeExpenseEntity == null) { return(BadRequest(new Response { IsSuccess = false, Message = Resource.TheTypeExpenseWasNotFound })); } string picturePath = string.Empty; if (TripDetailRequest.PictureArray != null && TripDetailRequest.PictureArray.Length > 0) { picturePath = _imageHelper.UploadImage(TripDetailRequest.PictureArray, "BillsPictures"); } var travelDetail = new TripDetailEntity { StartDate = TripDetailRequest.StartDate, BillValue = TripDetailRequest.BillValue, PicturePath = picturePath, Trip = await _context.Trips.FirstOrDefaultAsync(u => u.Id == TripDetailRequest.TripId), TripExpenseType = await _context.TripExpenseTypes.FirstOrDefaultAsync(c => c.Id == TripDetailRequest.TripExpenseTypeId), }; _context.TripDetails.Add(travelDetail); await _context.SaveChangesAsync(); return(Ok(new Response { IsSuccess = true, Message = Resource.TheExpensesOfTheTripHasBeenSaved })); }