Пример #1
0
        public IActionResult AddTicketMessage(AddTicketMessageReq model)
        {
            var response = _ticketService.AddTicketMessage(model);

            if (response == null)
            {
                return(BadRequest(new { message = "Wrong data to save." }));
            }

            return(Ok(response));
        }
Пример #2
0
        public TicketMessage AddTicketMessage(AddTicketMessageReq model)
        {
            //Take user and check if it exists
            var user = _context.Users
                       .Where(u => u.Id == model.UserId)
                       .SingleOrDefault();

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

            //Take ticket and check if it exists
            var ticket = _context.Tickets
                         .Where(t => t.Id == model.TicketId)
                         .SingleOrDefault();

            if (ticket == null || ticket.Status == "closed")
            {
                return(null);
            }

            //Open ticket if the replyer is admin
            if (user.IsAdmin == true)
            {
                ticket.Status = "opened";
            }
            //Update ticket's last action date
            ticket.LastActionDate = DateTime.Now;
            _context.SaveChanges();

            //Build ticket message data
            TicketMessage ticketMessage = new TicketMessage {
                Id      = 0,
                Date    = DateTime.Now,
                User    = user,
                Ticket  = ticket,
                Content = model.Content
            };

            //Add ticket message
            _context.TicketMessages.Add(ticketMessage);
            _context.SaveChanges();

            //Check if it is added succesfuly
            if (ticketMessage.Id == 0)
            {
                return(null);
            }

            return(ticketMessage);
        }