Ejemplo n.º 1
0
        public async Task <ActionResult> BookingAsync([FromBody] BookingCM model)
        {
            if (!CanBook(model.CustomerId))
            {
                return(Ok(false));
            }

            try
            {
                var customer = _customerService.GetCustomer(model.CustomerId);
                if (customer != null)
                {
                    string        currentUserName = (await _userManager.GetUserAsync(User)).UserName;
                    var           tickets         = _ticketService.GetTickets(t => t.BlockId == model.BlockId).ToList();
                    List <Ticket> newTickets      = new List <Ticket>();
                    //Check xem Khach hang này đã đặt trong block nay chưa
                    foreach (var item in tickets)
                    {
                        if (item.CustomerId == null)
                        {
                            newTickets.Add(item);
                        }
                        else if (item.CustomerId.Equals(model.CustomerId.ToString()))
                        {
                            return(Ok(false));
                        }
                    }

                    if (newTickets.Count != 0)
                    {
                        newTickets[0].CustomerId  = model.CustomerId.ToString();
                        newTickets[0].BookingDate = DateTime.Now;
                        newTickets[0].Note        = model.Note;
                        _ticketService.UpdateTicket(newTickets[0], currentUserName);
                        if (newTickets.Count == 1) // Chỉ còn 1 ticket thì update isFull
                        {
                            var block = _blockService.GetBlock(model.BlockId);
                            block.IsFull = true;
                            _blockService.UpdateBlock(block, currentUserName);
                        }
                        _blockService.Save();
                        _hubContext.Clients.All.SendAsync("Notify", JsonConvert.SerializeObject(new { Mess = "Co nguoi moi dat lich" }));
                        return(Ok(true));
                    }
                    else
                    {
                        return(Ok(false));
                    }
                }
                else
                {
                    return(BadRequest("Customer does not existed!"));
                }
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }
        public async Task <ActionResult <BookingCM> > CreateBooking([FromBody] BookingCM serviceModel)
        {
            //TODO: Implements BookingDetail.GetById(int id) does not exist, return BadRequest()
            //TODO: Implements GaleryId.GetById(int id) does not exist, return BadRequest()

            DateTime createDate = DateTime.Now;
            DateTime updateDate = DateTime.Now;

            Booking crtBooking = _mapper.Map <Booking>(serviceModel);

            try
            {
                crtBooking.CreateDate = createDate;
                crtBooking.UpdateDate = updateDate;
                crtBooking.Status     = "Mới";
                var data = new Dictionary <String, String>();
                await _service.AddAsync(crtBooking);

                await _service.Save();

                Account customerAccount = await _accountService.GetByIdAsync(crtBooking.CustomerAccountId);

                data.Add("notiType", "booking_created");
                data.Add("bookingId", crtBooking.Id.ToString());
                _ = _pushNotificationService.SendMessage(
                    "Bạn nhận được đơn hàng mới",
                    "Khách hàng: " + customerAccount.DisplayName + "\n" +
                    "Địa chỉ: " + crtBooking.EndAddress,
                    "booking_created_id_" + crtBooking.BeautyArtistAccountId,
                    data
                    );
                Console.WriteLine("abc");
            }
            catch (Exception)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }
            return(CreatedAtAction("GetBookingById", new { id = crtBooking.Id }, crtBooking));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> ChangeBookingAsync([FromBody] BookingCM model)
        {
            if (CanBook(model.CustomerId))
            {
                return(Ok(false));
            }
            try
            {
                var customer = _customerService.GetCustomer(model.CustomerId);
                if (customer != null)
                {
                    string currentUserName = (await _userManager.GetUserAsync(User)).UserName;

                    //BlockId da duoc check isFull va time lon hon time hien tai
                    var tickets = _ticketService.GetTickets(t => t.BlockId == model.BlockId).ToList();

                    List <Ticket> newTickets = new List <Ticket>();
                    //Check xem Khach hang này đã đặt trong block nay chưa
                    foreach (var item in tickets)
                    {
                        if (item.CustomerId == null)
                        {
                            newTickets.Add(item);
                        }
                        else if (item.CustomerId.Equals(model.CustomerId.ToString()))
                        {
                            return(Ok(false));
                        }
                    }

                    if (newTickets.Count != 0)
                    {
                        //Xoa customer tu Ticket cu va check xem isFull=true thi set isFull = false
                        var currentTicket = _ticketService.GetTickets(t => t.CustomerId == model.CustomerId.ToString() &&
                                                                      t.Block.Date.Date >= DateTime.Now.Date &&
                                                                      t.Status == 0).FirstOrDefault();
                        currentTicket.CustomerId = null;
                        currentTicket.Note       = null;
                        if (currentTicket.Block.IsFull)
                        {
                            currentTicket.Block.IsFull = false;
                        }
                        _ticketService.UpdateTicket(currentTicket, currentUserName);

                        //Gan customer vao ticket va check xem neu con 1 ticket thi set block isFull = true
                        newTickets[0].CustomerId  = model.CustomerId.ToString();
                        newTickets[0].BookingDate = DateTime.Now;
                        newTickets[0].Note        = model.Note;
                        _ticketService.UpdateTicket(newTickets[0], currentUserName);
                        if (newTickets.Count == 1) // Chỉ còn 1 ticket thì update isFull
                        {
                            var block = _blockService.GetBlock(model.BlockId);
                            block.IsFull = true;
                            _blockService.UpdateBlock(block, currentUserName);
                        }
                        _blockService.Save();
                        return(Ok(true));
                    }
                    else
                    {
                        return(Ok(false));
                    }
                }
                else
                {
                    return(BadRequest("Customer does not existed!"));
                }
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }