Exemplo n.º 1
0
        public IHttpActionResult PutTicket(int id, Ticket ticket)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != ticket.Id)
            {
                return(BadRequest());
            }

            db.Entry(ticket).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TicketExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemplo n.º 2
0
 public int Alterar(Status status)
 {
     try
     {
         using (HelpDeskEntities contexto = new HelpDeskEntities(Util.ConnectionString))
         {
             contexto.Entry(status).State = System.Data.EntityState.Modified;
             return(contexto.SaveChanges());
         }
     }
     catch (Exception)
     {
         return(-1);
     }
 }
Exemplo n.º 3
0
        private async Task <Tuple <bool, string> > IsTicketUpdated(TicketViewModel model)
        {
            if (string.IsNullOrWhiteSpace(SessionVar.GetString("displayName")))
            {
                Redirect("~/Account/Login");
            }

            StringBuilder errors       = new StringBuilder();
            Guid          id           = model.TicketID;
            bool          isSuccessful = false;

            try
            {
                #region validate inputs
                if (model.TicketID == null)
                {
                    errors.AppendLine("Ticket ID is required.");
                }
                if (string.IsNullOrWhiteSpace(model.Description))
                {
                    errors.AppendLine("Description is required.");
                }
                if (model.FileAttachments != null)
                {
                    string[] unsupportedFileTypes = new[] { "exe" };
                }
                #endregion

                if (errors.Length > 0)
                {
                    isSuccessful = false;
                }
                else
                {
                    // sanitize data
                    model.Description = SecurityHelper.Sanitize(model.Description);

                    #region save data
                    using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                    {
                        using (HelpDeskEntities dataContext = new HelpDeskEntities())
                        {
                            Ticket ticket = await dataContext.Tickets.FirstOrDefaultAsync(p => p.TicketID.Equals(model.TicketID));

                            if (ticket == null)
                            {
                                errors.AppendLine("Unknown ticket details");
                            }

                            if (errors.Length == 0)
                            {
                                model.Subject    = ticket.Subject;
                                model.OwnerEmail = ticket.OwnerEmail;

                                ticket.Status     = model.Status;
                                ticket.ModifiedBy = User.Identity.Name;

                                dataContext.Entry(ticket).State = EntityState.Modified;

                                if (await dataContext.SaveChangesAsync() > 0)
                                {
                                    Guid ticketNoteID = Guid.NewGuid();

                                    #region ticket note
                                    TicketNote ticketNote = new TicketNote()
                                    {
                                        TicketNoteID = ticketNoteID,
                                        TicketID     = model.TicketID,
                                        Note         = model.Description,
                                        CreatedBy    = User.Identity.Name,
                                        CreationDate = DateTime.Now,
                                        ModifiedBy   = User.Identity.Name,
                                        ModifiedDate = DateTime.Now,
                                        IsDeleted    = false
                                    };

                                    dataContext.TicketNotes.Add(ticketNote);

                                    if (await dataContext.SaveChangesAsync() > 0)
                                    {
                                        #region ticket files
                                        if (model.FileAttachments != null)
                                        {
                                            if (model.FileAttachments.Length > 0)
                                            {
                                                string targetPath = HttpContext.Server.MapPath(this.fileAttachmentPath);

                                                if (model.FileAttachments[0] != null)
                                                {
                                                    List <TicketFile> ticketFiles = new List <TicketFile>();

                                                    foreach (var file in model.FileAttachments)
                                                    {
                                                        string fileName = Path.Combine(targetPath, file.FileName);
                                                        file.SaveAs(fileName);

                                                        ticketFiles.Add(new TicketFile()
                                                        {
                                                            TicketFileID = Guid.NewGuid(),
                                                            TicketID     = ticket.TicketID,
                                                            FileName     = file.FileName,
                                                            FileForTOrN  = "N",
                                                            RefID        = ticketNoteID,
                                                            CreatedBy    = User.Identity.Name,
                                                            CreationDate = DateTime.Now,
                                                            ModifiedBy   = User.Identity.Name,
                                                            ModifiedDate = DateTime.Now,
                                                            IsDeleted    = false
                                                        });
                                                    }

                                                    dataContext.TicketFiles.AddRange(ticketFiles);

                                                    await dataContext.SaveChangesAsync();
                                                }
                                            }
                                        }
                                        #endregion
                                    }
                                    #endregion

                                    transactionScope.Complete();
                                    isSuccessful = true;
                                }
                                else
                                {
                                    errors.AppendLine("Unknow error occured.");
                                }
                            }
                        }
                    }
                    #endregion
                }
            }
            catch (DbEntityValidationException ex)
            {
                isSuccessful = false;
                HomeController.LogError(ex, HttpContext.Server.MapPath("~/Error_Log.txt"));
            }
            catch (Exception ex)
            {
                isSuccessful = false;
                errors.AppendLine(ex.Message);
                HomeController.LogError(ex, HttpContext.Server.MapPath("~/Error_Log.txt"));
            }

            return(Tuple.Create(isSuccessful, errors.ToString()));
        }