/// <summary> /// Posts the new ticket entry to the database /// </summary> /// <param name="td"></param> /// <param name="loggedInUser"></param> /// <returns></returns> public bool PostEntry(TicketData td, UserData loggedInUser) { try { using (var context = new TicketingSystemDBContext()) { IEnumerable<JobType> jobs = context.JobType; int jtypeID = 1; jtypeID = context.JobType.Where(j => j.JobName == td.JobType.JobName).FirstOrDefault().JobTypeId; int authorID = context.Users.Where(a => a.Auth0Uid == loggedInUser.user_id).FirstOrDefault().UserId; var worker = context.Users.Where(w => w.FullName == td.TicketWorker.FullName).FirstOrDefault(); int workerID; if (worker != null) { workerID = worker.UserId; td.TicketWorkerId = workerID; } else { throw new Exception("Error, Employee with name: " + worker.FullName + " not found in System"); } td.JobTypeId = jtypeID; td.TicketClosed = false; td.EntryAuthorId = authorID; td.WorkerName = td.TicketWorker.FullName; td.StartDate = DateTime.Today; //very important null assignment td.JobType = null; td.TicketWorker = null; td.EntryAuthor = null; context.TicketData.Add(td); context.SaveChanges(); int entryID = td.EntryId; TicketDataLogger tdl = new TicketDataLogger(); tdl.LogChange("new entry", "created new entry", entryID, authorID); } } catch (Exception e) { throw new HttpResponseException(Utility.CreateResponseMessage(e)); } return true; }
/// <summary> /// Posts the edited TicketData object to the database and logs the user who triggered it /// </summary> /// <param name="td">The ticket data instance to update</param> /// <param name="loggedInUser">The currently logged user</param> /// <returns></returns> public bool PostEditor(TicketData td, UserData loggedInUser) { try { using (var context = new TicketingSystemDBContext()) { int jtypeID; jtypeID = context.JobType.Where(j => j.JobName == td.JobType.JobName).FirstOrDefault().JobTypeId; int authorID = -1; int workerID; var author = context.Users.Where(a => a.Auth0Uid == loggedInUser.user_id).FirstOrDefault(); var worker = context.Users.Where(w => w.FullName == td.TicketWorker.FullName).FirstOrDefault(); if (author != null) { authorID = author.UserId; td.EntryAuthorId = authorID; } if (worker != null) { workerID = worker.UserId; td.TicketWorkerId = workerID; } td.JobTypeId = jtypeID; td.WorkerName = td.TicketWorker.FullName; //very important null assignment td.JobType = null; td.TicketWorker = null; td.EntryAuthor = null; context.TicketData.Update(td); context.SaveChanges(); TicketDataLogger tdl = new TicketDataLogger(); tdl.LogChange("new edits", "edited entry", td.EntryId, authorID); } } catch (Exception e) { throw new HttpResponseException(Utility.CreateResponseMessage(e)); } return(true); }
/// <summary> /// Deletes the specified ticket data entry /// </summary> /// <param name="entryId">The unique id of the ticket to be deleted</param> /// <param name="loggedInUser"></param> /// <returns></returns> public bool DeleteEntry(string entryId, UserData loggedInUser) { try { using (var db = new TicketingSystemDBContext()) { TicketData td = db.TicketData.Find(int.Parse(entryId)); db.TicketData.Remove(td); db.SaveChanges(); var author = db.Users.Where(auth => auth.Auth0Uid == loggedInUser.user_id).FirstOrDefault(); TicketDataLogger tdl = new TicketDataLogger(); tdl.LogChange("delete", "deleted entry with ID: " + td.EntryId, td.EntryId, author.UserId); } } catch (Exception e) { throw new HttpResponseException(Utility.CreateResponseMessage(e)); } return(true); }