예제 #1
0
        public async Task PickMoreOrders(CoffeeService coffee, User user, string text, SlackResponse response)
        {
            if (!int.TryParse(text, out var count))
            {
                throw new NotWellFormedException();
            }
            if (count <= 0)
            {
                throw new NotWellFormedException();
            }

            var at = DateTime.Now;

            var picked = await coffee.PickMoreOrderAsync(count, at);

            if (picked.Count <= 0)
            {
                throw new NoOneToPickException();
            }

            var orders = await coffee.GetOrdersAsync(at);

            var candidatesCount = picked.Count + orders.Count(o => o.PickedAt <= DateTime.MinValue);
            var sb = new StringBuilder($"<추가 당첨자 명단> {candidatesCount}명 중에 {picked.Count}명").AppendLine();
            await sb.AppendOrders(coffee, picked);

            var responseText = sb.ToString();

            response
            .Ephemeral("추첨하였습니다")
            .InChannel(responseText, "manager")
            .InChannel(responseText, "user");
        }
예제 #2
0
        public async void CoffeeService()
        {
            using var context = TestCoffeeContext.CreateContext();
            context.SetUsers();
            context.SetMenus();
            await context.SaveChangesAsync();

            var at      = DateTime.Now;
            var service = new CoffeeService(context);

            // 이전 주문이 없어서 실패
            await Assert.ThrowsAsync <BadRequestException>(() => service.MakeOrderAsync("id1", "", at));

            // 없는 메뉴이기 때문에 실패
            await Assert.ThrowsAsync <MenuNotFoundException>(() => service.MakeOrderAsync("id1", "없는메뉴", at));

            var order = await service.MakeOrderAsync("id1", "메뉴1", at);

            await context.SaveChangesAsync();

            at = at.AddSeconds(10);

            // 아직 추첨을 안했기 때문에 완성 실패
            await Assert.ThrowsAsync <BadRequestException>(service.CompleteOrderAsync);

            var pickedOrders = await service.PickOrderAsync(10, at.AddSeconds(10));

            await context.SaveChangesAsync();

            at = at.AddSeconds(10);

            // 이미 추첨을 했기 때문에 추첨 불가
            await Assert.ThrowsAsync <BadRequestException>(() => service.PickOrderAsync(10, at));

            Assert.Single(pickedOrders);
            Assert.Equal("id1", pickedOrders[0].UserId);
            Assert.Equal("메뉴1", pickedOrders[0].MenuId);

            // 추가 신청
            await service.MakeOrderAsync("id2", "메뉴2", at);

            await context.SaveChangesAsync();

            at = at.AddSeconds(10);

            // 추가 추첨
            var pickedMore = await service.PickMoreOrderAsync(2, at);

            await context.SaveChangesAsync();

            Assert.Single(pickedMore);
            Assert.Equal("id2", pickedMore[0].UserId);

            var completedOrders = await service.CompleteOrderAsync();

            await context.SaveChangesAsync();

            Assert.Equal(2, completedOrders.Count);
            Assert.Equal("id1", completedOrders[0].UserId);
            Assert.Equal("메뉴1", completedOrders[0].MenuId);

            var orders = await context.Orders.ToListAsync();

            Assert.Empty(orders);

            var completeds = await context.CompletedOrders.ToListAsync();

            Assert.Equal(2, completeds.Count);

            var user = await context.Users.FindAsync("id1");

            Assert.Equal(500, (int)user.Deposit);

            user = await context.Users.FindAsync("id2");

            Assert.Equal(-500, (int)user.Deposit);
        }