public async Task <IActionResult> AddOwner(OwnerDto ownerDto) { var ownerToAdd = new Owner(); _mapper.Map(ownerDto, ownerToAdd); var userIdFromClaims = User.Claims.FirstOrDefault().Value; if (Int32.TryParse(userIdFromClaims, out int currentUser)) { ownerToAdd.ModifiedBy = currentUser; } else { ownerToAdd.ModifiedBy = 0; } await AddAddress(ownerToAdd, ownerDto); _repo.Add <Owner>(ownerToAdd); var ownerAddedSuccess = await _repo.Commit(); if (!ownerAddedSuccess) { return(StatusCode(500)); } //add cottage owner entitie(s) return(CreatedAtRoute("GetOwnerById", new { id = ownerToAdd.Id }, new { id = ownerToAdd.Id })); }
private Holiday CreateHolidayFromPendingBooking(PendingBooking pendingBooking, Cottage cottage, Customer customer, PaymentIntent paymentIntent) { var holiday = new Holiday { Customer = customer, Cottage = cottage, NumberOfAdults = pendingBooking.NumberOfAdults, NumberOfChildren = pendingBooking.NumberOfChildren, NumberOfInfants = pendingBooking.NumberOfInfants, FirstNight = pendingBooking.ArrivalDate, LastNight = pendingBooking.LeavingDate, TotalCost = pendingBooking.TotalCost, CreatedBy = pendingBooking.CreatedBy }; holiday.DepositPaid = paymentIntent.AmountReceived / 100; if (holiday.DepositPaid == holiday.TotalCost) { holiday.PaymentStatus = PaymentStatus.PaidInFull; } else { holiday.PaymentStatus = PaymentStatus.DepositPaid; } _repo.Add <Holiday>(holiday); return(holiday); }
public async Task <IActionResult> CreatePendingBooking([FromBody] PendingBookingDto pendingBookingDto) { var pendingBooking = _mapper.Map <PendingBooking>(pendingBookingDto); var userIdFromClaims = User.Claims.FirstOrDefault().Value; if (Int32.TryParse(userIdFromClaims, out int currentUser)) { pendingBooking.CreatedBy = currentUser; } else { pendingBooking.CreatedBy = 0; } _repo.Add <PendingBooking>(pendingBooking); var pbAddSuccess = await _repo.Commit(); if (!pbAddSuccess) { return(StatusCode(500)); } var objectToReturn = new { id = pendingBooking.Id }; return(Ok(objectToReturn)); }
public async Task <IActionResult> AddCottage([FromForm] CottageAddUpdateDto cottageAddUpdateDto) { Cottage cottageToAdd = _mapper.Map <Cottage>(cottageAddUpdateDto); var cottageRegion = await _repo.GetRegionById(cottageAddUpdateDto.RegionId); cottageToAdd.Region = cottageRegion; var userIdFromClaims = User.Claims.FirstOrDefault().Value; if (Int32.TryParse(userIdFromClaims, out int currentUser)) { cottageToAdd.ModifiedBy = currentUser; } else { cottageToAdd.ModifiedBy = 0; } _repo.Add <Cottage>(cottageToAdd); var cottageAddedSuccess = await _repo.Commit(); if (!cottageAddedSuccess) { return(StatusCode(500)); } if (cottageAddUpdateDto.PhotoFile != null) { var photoUrl = await SavePhoto(cottageAddUpdateDto.PhotoFile, cottageToAdd.Id, cottageToAdd.Name); if (!String.IsNullOrEmpty(photoUrl)) { cottageToAdd.Photo = photoUrl; } } //add cottage owner entitie(s) List <CottageOwner> cottageOwners = cottageAddUpdateDto.OwnerIds.Select(o => new CottageOwner { CottageId = cottageToAdd.Id, OwnerId = o }).ToList(); _repo.AddCottageOwners(cottageOwners); var cottageOwnersAdded = await _repo.Commit(); if (!cottageOwnersAdded) { return(StatusCode(500)); } return(CreatedAtRoute("GetCottageById", new { id = cottageToAdd.Id }, new { id = cottageToAdd.Id })); }
public async Task <IActionResult> RegisterCustomer(RegisterDto registerDto) { var user = await _userManager.FindByEmailAsync(registerDto.Email); if (user != null) { return(Conflict("There is already an account with this email")); } var customer = new Customer() { FirstName = registerDto.FirstName, LastName = registerDto.LastName }; _repo.Add <Customer>(customer); var customerAdded = await _repo.Commit(); if (!customerAdded) { return(StatusCode(500)); } user = new User() { Email = registerDto.Email, UserName = registerDto.Email, PersonId = customer.Id }; var result = _userManager.CreateAsync(user, registerDto.Password).Result; if (result != IdentityResult.Success) { return(StatusCode(500)); } var newUser = _userManager.FindByEmailAsync(user.Email).Result; _userManager.AddToRolesAsync(newUser, new string[] { "Customer" }).Wait(); return(Ok()); }