public override CommonResponse <TicketVM> UpdateData(TicketVM data, string transactionId)
        {
            CommonResponse <TicketVM> response = ValidateParameter(data);

            if (response != null)
            {
                return(response);
            }

            User         assignee = DbContext.Users.Where(u => u.Id == data.AssigneeId).FirstOrDefault();
            Category     category = DbContext.Categories.Where(u => u.Id == data.CategoryID).FirstOrDefault();
            TicketStatus status   = DbContext.TicketStatuses.Where(u => u.Id == data.StatusID).FirstOrDefault();

            if (data?.Id == null)
            {
                return(new CommonResponse <TicketVM>
                {
                    Status = false,
                    Code = "E-002",
                    Message = "Invalid update data, please fill the ID!",
                    Data = new TicketVM[] { data },
                });
            }

            Ticket lastData = DbContext.Tickets.Where(u => u.Id == data.Id).FirstOrDefault();

            if (lastData == null)
            {
                return(new CommonResponse <TicketVM>
                {
                    Status = false,
                    Code = "E-002",
                    Message = string.Format("Data not found for ID {0}, please fill the right ID!", data?.Id),
                    Data = new TicketVM[] { data },
                });
            }

            lastData.Name        = data.Name;
            lastData.Description = data.Description;
            lastData.CreatedDate = data.CreatedDate;

            if (assignee != null)
            {
                lastData.AssigneeId = assignee.Id;
            }

            if (category != null)
            {
                lastData.CategoryId = category.Id;
            }

            if (status != null)
            {
                lastData.StatusId = status.Id;
            }

            //DbContext.Add(lastData);

            int historySequence = lastData?.TicketHistories?
                                  .Select(u => u.Seq)
                                  .OrderByDescending(u => u)
                                  .FirstOrDefault() ?? 0;

            TicketHistory ticketHistory = new TicketHistory();

            ticketHistory.FillFromTicket(
                lastData,
                transactionId,
                historySequence + 1,
                TableTransactionOperation.Update
                );

            DbContext.Add(ticketHistory);

            if (DbContext.SaveChanges() > 0)
            {
                return(new CommonResponse <TicketVM>
                {
                    Status = true,
                    Code = "S",
                    Message = "Update Data is Success!",
                    Data = new TicketVM[] { data },
                });
            }
            else
            {
                return(new CommonResponse <TicketVM>
                {
                    Status = false,
                    Code = "E-003",
                    Message = "Update Data Failed! Please Contact an Web Administrator!",
                    Data = new TicketVM[] { data },
                });
            }
        }
        public override CommonResponse <TicketVM> DeleteData(TicketVM data, string transactionId)
        {
            IQueryable <Ticket> lastDataContext = DbContext.Tickets.AsQueryable();

            if (string.IsNullOrEmpty(data?.Id))
            {
                return(new CommonResponse <TicketVM>
                {
                    Status = false,
                    Code = "E-001",
                    Message = "Please fill the ID!",
                    Data = new TicketVM[] { data },
                });
            }

            lastDataContext = lastDataContext.Where(u =>
                                                    u.Id.Equals(data.Id)
                                                    );

            Ticket lastData = lastDataContext.FirstOrDefault();

            if (lastData == null)
            {
                return(new CommonResponse <TicketVM>
                {
                    Status = false,
                    Code = "E-002",
                    Message = string.Format("Data not found for ID {0}, please fill the right ID!", data?.Id),
                    Data = new TicketVM[] { data },
                });
            }

            int historySequence = lastData?.TicketHistories?
                                  .Select(u => u.Seq)
                                  .OrderByDescending(u => u)
                                  .FirstOrDefault() ?? 0;

            lastData.IsDeleted = true;

            TicketHistory ticketHistory = new TicketHistory();

            ticketHistory.FillFromTicket(
                lastData,
                transactionId,
                historySequence + 1,
                TableTransactionOperation.Delete
                );

            DbContext.Add(ticketHistory);

            if (DbContext.SaveChanges() > 0)
            {
                return(new CommonResponse <TicketVM>
                {
                    Status = true,
                    Code = "S",
                    Message = "Delete Data is Success!",
                    Data = new TicketVM[] { data },
                });
            }
            else
            {
                return(new CommonResponse <TicketVM>
                {
                    Status = false,
                    Code = "E-003",
                    Message = "Delete Data Failed! Please Contact an Web Administrator!",
                    Data = new TicketVM[] { data },
                });
            }
        }
        public override CommonResponse <TicketVM> SaveData(TicketVM data, string transactionId)
        {
            CommonResponse <TicketVM> response = ValidateParameter(data);

            if (response != null)
            {
                return(response);
            }

            User         assignee = DbContext.Users.Where(u => u.Id == data.AssigneeId).FirstOrDefault();
            User         owner    = DbContext.Users.Where(u => u.Id == data.OwnerID).FirstOrDefault();
            Category     category = DbContext.Categories.Where(u => u.Id == data.CategoryID).FirstOrDefault();
            TicketStatus status   = DbContext.TicketStatuses.Where(u => u.Id == data.StatusID).FirstOrDefault();

            string error_message = "";

            if (data?.CreatedDate == null)
            {
                error_message += "Created Date must be filled!, ";
            }

            if (string.IsNullOrEmpty(data?.Name))
            {
                error_message += "Name must be filled!, ";
            }

            /*
             * if (assignee == null)
             * {
             *  error_message += "Assignee must be filled!, ";
             * }
             *
             * if (owner == null)
             * {
             *  error_message += "Owner must be filled!, ";
             * }
             */

            if (category == null)
            {
                error_message += "Category must be filled!, ";
            }

            if (status == null)
            {
                error_message += "Status must be filled!, ";
            }

            if (!string.IsNullOrEmpty(error_message))
            {
                return(new CommonResponse <TicketVM>
                {
                    Status = false,
                    Code = "E-002",
                    Message = error_message,
                    Data = new TicketVM[] { data },
                });
            }

            string lastId = DbContext.Tickets.Select(u => u.Id).OrderByDescending(u => u).FirstOrDefault();

            Ticket ticketData = new Ticket
            {
                Id          = CreateNewTicketId(lastId),
                Name        = data?.Name,
                Description = data.Description,
                CreatedDate = data.CreatedDate,

                AssigneeId = assignee.Id,
                OwnerId    = owner.Id,
                CategoryId = category.Id,
                StatusId   = status.Id,
            };

            DbContext.Add(ticketData);

            TicketHistory ticketHistory = new TicketHistory();

            ticketHistory.FillFromTicket(
                ticketData,
                transactionId,
                1,
                TableTransactionOperation.Insert
                );

            DbContext.Add(ticketHistory);

            if (DbContext.SaveChanges() > 0)
            {
                data.Id = ticketData.Id;

                return(new CommonResponse <TicketVM>
                {
                    Status = true,
                    Code = "S",
                    Message = "Save Data is Success!",
                    Data = new TicketVM[] { data },
                });
            }
            else
            {
                return(new CommonResponse <TicketVM>
                {
                    Status = true,
                    Code = "E-003",
                    Message = "Save Data Failed! Please Contact an Web Administrator!",
                    Data = new TicketVM[] { data },
                });
            }
        }