Beispiel #1
0
        public void AddReservation(Reservation reservation)
        {
            string key      = "room:" + reservation.Room.Id.ToString() + ":reservations";
            var    response = redis.Get(key);

            if (response != null)
            {
                var reservations = jsonS.Deserialize <List <ReservationObject> >(response);
                ReservationObject newReservation = new ReservationObject
                {
                    RoomId        = reservation.Room.Id,
                    ReservationId = reservation.Id,
                    DateStart     = reservation.DateStart,
                    DateEnd       = reservation.DateEnd
                };
                reservations.Add(newReservation);
                redis.Set(key, jsonS.Serialize(reservations));
            }
            else
            {
                var reservations = new List <Object>();
                ReservationObject newReservation = new ReservationObject
                {
                    RoomId        = reservation.Room.Id,
                    ReservationId = reservation.Id,
                    DateStart     = reservation.DateStart,
                    DateEnd       = reservation.DateEnd
                };
                reservations.Add(newReservation);
                redis.Set(key, jsonS.Serialize(reservations));
            }
        }
Beispiel #2
0
 public AppState(HttpClient httpInstance)
 {
     http = httpInstance;
     if (Reservation == null)
     {
         Reservation = new Reservation();
     }
     if (ReservationObject == null)
     {
         ReservationObject = new ReservationObject();
     }
 }
Beispiel #3
0
        private async Task <Guid> MakeReservationHelper(Guid id)
        {
            var reservation = new ReservationObject {
                AccountId = id, Amount = 5
            };
            var reservationContent = new StringContent(JsonConvert.SerializeObject(reservation), Encoding.UTF8, "application/json");
            var httpResponse       = await _client.PostAsync("/api/Reservation", reservationContent);

            httpResponse.EnsureSuccessStatusCode();
            var reservationId = await httpResponse.Content.ReadAsAsync <Guid>(new[] { new JsonMediaTypeFormatter() });

            return(reservationId);
        }
Beispiel #4
0
        public ReservationObjectDto MapFromEntity(ReservationObject reservationObject)
        {
            if (reservationObject != null)
            {
                ID = reservationObject.ID;
                //Active = reservationObject.Active;
                Name                   = reservationObject.Name;
                Description            = reservationObject.Description;
                MaximumReservationTime = ConvertFromMinutesToDays(reservationObject.MaximumReservationTime.GetValueOrDefault());
                ObjectOwnerID          = reservationObject.ObjectOwnerID;
                ObjectOwnerName        = reservationObject.ObjectOwner?.Name;
                ObjectOwnerDescription = reservationObject.ObjectOwner?.Description;
            }

            return(this);
        }
Beispiel #5
0
        public ReservationObject MapToEntity(ReservationObject reservationObject)
        {
            if (reservationObject == null)
            {
                reservationObject = new ReservationObject();
            }

            ReservationObject reservationObjectEntity = reservationObject;

            reservationObjectEntity.Name                   = Name;
            reservationObjectEntity.Description            = Description;
            reservationObjectEntity.MaximumReservationTime = ConvertFromDaysToMinutes(MaximumReservationTime.GetValueOrDefault());
            reservationObjectEntity.ObjectOwnerID          = ObjectOwnerID;

            return(reservationObjectEntity);
        }
Beispiel #6
0
        public async Task <ActionResult <ReservationResult> > PostReservation(ReservationObject reservationObject)
        {
            //if (!RequestHelper.ValidateId(reservationObject.AccountId, Request, _env))
            //    return BadRequest("HeaderId and Id are not equal");
            try
            {
                var account = await _context.Accounts.FirstOrDefaultAsync(x => x.OwnerId == reservationObject.AccountId);

                if (account == null)
                {
                    _logger.LogWarning(@"AccountId {AccountId} does not exist", reservationObject.AccountId);
                    return(new ReservationResult {
                        Valid = false, ErrorMessage = $"AccountId {reservationObject.AccountId} does not exist"
                    });
                }
                if (account.Balance < reservationObject.Amount)
                {
                    _logger.LogInformation("The account {AccountId} only got a balance of {balance}, but the request is for {Amount}", reservationObject.AccountId, account.Balance, reservationObject.Amount);
                    return(new ReservationResult {
                        Valid = false, ErrorMessage = $"The balance is {account.Balance}, but the request is for {reservationObject.Amount}"
                    });
                }

                account.Balance = account.Balance - reservationObject.Amount;
                var reservation = new Reservation {
                    Amount = reservationObject.Amount, OwnerAccount = account
                };
                _context.Reservations.Add(reservation);

                await _context.SaveChangesAsync();

                _rabbitMqClient.SendMessage(new HistoryMessage {
                    Event = "CreatedReservation", EventMessage = $"Reserved ${reservationObject.Amount} for buying shares with reservation id {reservation.Id}", User = reservationObject.AccountId, Timestamp = DateTime.UtcNow
                });
                _logger.LogInformation("Successfully reserved {Amount} from {@Account}", reservationObject.Amount, account);
                TotalMoneyReserved.Inc(reservationObject.Amount);
                return(new ReservationResult {
                    Valid = true, ReservationId = reservation.Id, ErrorMessage = string.Empty
                });
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Failed to Reserve money");
                throw;
            }
        }
Beispiel #7
0
        public async Task GetReservationObject(int Id)
        {
            ReservationObject = await http.GetJsonAsync <ReservationObject>($"{apiUrl}/reservationObject/{Id}");

            NotifyStateChanged();
        }
		public async Task<int> Update(int id, ReservationObjectDto dto)
		{
			ReservationObject reservation = await UnitOfWork.Repository<ReservationObject>().GetByIDAsync(id);
			UnitOfWork.Repository<ReservationObject>().Update(dto.MapToEntity(reservation));
			return await UnitOfWork.CommitAsync();
		}
		public async Task<ReservationObjectDto> GetAsync(int id)
		{
			ReservationObject reservation = await UnitOfWork.Repository<ReservationObject>().GetByIDAsync(id);

			return reservation == null ? null : new ReservationObjectDto().MapFromEntity(reservation);
		}