예제 #1
0
        public async Task <BookingMonth> Find(BookingMonthId aggregateId)
        {
            var query = $@"
                SELECT
                    id as Id,
                    booking as Booking
                FROM laundry.bookings
                WHERE id = @id";

            using (var connection = new NpgsqlConnection(_options.ConnectionString))
            {
                var pgDocument = await connection.QuerySingleOrDefaultAsync <BookingMonthPostgresDocument>(query, new { id = aggregateId.Value });

                if (pgDocument == null)
                {
                    return(null);
                }

                var dataModel = JsonConvert.DeserializeObject <BookingMonthDataModel>(pgDocument.Booking);

                if (dataModel == null)
                {
                    return(null);
                }

                var aggregate = BookingMonthMapper.From(dataModel);

                return(aggregate);
            }
        }
예제 #2
0
        public async Task <IActionResult> PostBooking(
            [FromRoute] int year,
            [FromRoute] int month,
            [FromRoute] int day,
            [FromBody] BookingInputModel input)
        {
            var bookingMonthId = BookingMonthId.From(new DateTime(year, month, day));
            var bookingMonth   = await _repository.Find(bookingMonthId);

            if (bookingMonth == null)
            {
                bookingMonth = new BookingMonth(bookingMonthId, year, month, new Dictionary <int, BookingDay>());
            }

            var userId = UserId.With(HttpContext.GetUserId().Value);
            var slot   = (Slot)input.Slot;

            bookingMonth.AddBooking(userId, day, slot);

            await _repository.Upsert(bookingMonth);

            var responseModel = await _factory.From(bookingMonth.BookingDays[day].Bookings[slot], input.Slot);

            return(CreatedAtAction("GetBookingSlot", new { year = year, month = month, day = day, slot = input.Slot, }, responseModel));
        }
예제 #3
0
        public async Task <ActionResult <BookingMonthResponseModel> > GetBookings(int year, int month)
        {
            var bookingMonthId = BookingMonthId.From(new DateTime(year, month, 1));
            var bookingMonth   = await _repository.Find(bookingMonthId);

            if (bookingMonth == null)
            {
                return(NotFound());
            }

            var responseModel = await _factory.From(bookingMonth);

            return(responseModel);
        }
예제 #4
0
        public static BookingMonth From(BookingMonthDataModel dataModel)
        {
            if (dataModel == null)
            {
                return(null);
            }

            var aggregateId = BookingMonthId.With(dataModel.Id);

            return(new BookingMonth(
                       aggregateId,
                       dataModel.Year,
                       dataModel.Month,
                       From(dataModel.BookingDays)));
        }
예제 #5
0
        public async Task <ActionResult <UserBookingResponseModel> > GetBookingSlot(int year, int month, int day, int slot)
        {
            var bookingMonthId = BookingMonthId.From(new DateTime(year, month, day));
            var bookingMonth   = await _repository.Find(bookingMonthId);

            if (bookingMonth == null)
            {
                return(NotFound());
            }

            var slotEnum = (Slot)slot;

            if (bookingMonth.BookingDays.ContainsKey(day) && bookingMonth.BookingDays[day].Bookings.ContainsKey(slotEnum))
            {
                return(await _factory.From(bookingMonth.BookingDays[day].Bookings[slotEnum], slot));
            }

            return(NotFound());
        }