/// <summary> /// This method updates a specified timespan's information in the database /// </summary> /// <param name="id">The SpanId of the timespan to be updated</param> /// <param name="request">The request that contains the timespan's new information</param> /// <returns>A bool indicating whether the operation was a success</returns> public bool UpdateTimeSpan(int id, UpdateTimeSpanRequest request) { using (helpdesksystemContext context = new helpdesksystemContext()) { Timespans timespan = context.Timespans.FirstOrDefault(t => t.SpanId == id); if (timespan == null) { return(false); } Timespans existingTimespan = null; existingTimespan = context.Timespans.FirstOrDefault(t => t.Name == request.Name); // Update if no timespan exists matching the requesting name. // Update anyway if the names match but the the existing timespan is the timespan we want to update. if (existingTimespan == null || existingTimespan.SpanId == id) { timespan.Name = request.Name; timespan.StartDate = request.StartDate; timespan.EndDate = request.EndDate; context.SaveChanges(); } else { throw new DuplicateNameException("The nickname " + request.Name + " already exists!"); } } return(true); }
/// <summary> /// This method adds a timespan to the database. /// </summary> /// <param name="request"></param> /// <returns></returns> public int AddTimeSpan(AddTimeSpanRequest request) { int spanId; Timespans timespan = new Timespans { HelpdeskId = request.HelpdeskId, Name = request.Name, StartDate = request.StartDate, EndDate = request.EndDate }; using (var context = new helpdesksystemContext()) { Timespans existingTimespan = null; existingTimespan = context.Timespans.FirstOrDefault(t => t.Name == timespan.Name); if (existingTimespan == null) { context.Timespans.Add(timespan); context.SaveChanges(); spanId = timespan.SpanId; } else { throw new DuplicateNameException("The nickname " + request.Name + " already exists!"); } } return(spanId); }
/// <summary> /// Converts the timespan DTO to a DAO to interact with the database /// </summary> /// <param name="timespanDTO">The DTO for the timespan</param> /// <returns>The DAO for the timespan</returns> private Timespans timespanDTO2DAO(TimeSpanDTO timespanDTO) { Timespans timespan = new Timespans { SpanId = timespanDTO.SpanId, HelpdeskId = timespanDTO.HelpdeskId, Name = timespanDTO.Name, StartDate = timespanDTO.StartDate, EndDate = timespanDTO.EndDate }; return(timespan); }
/// <summary> /// Converts the timespan DAO to a DTO to send to the front end /// </summary> /// <param name="timespan">The DAO for the timespan</param> /// <returns>The DTO for the timespan</returns> private TimeSpanDTO timespanDAO2DTO(Timespans timespan) { TimeSpanDTO timespanDTO = new TimeSpanDTO { SpanId = timespan.SpanId, HelpdeskId = timespan.HelpdeskId, Name = timespan.Name, StartDate = timespan.StartDate, EndDate = timespan.EndDate }; return(timespanDTO); }
/// <summary> /// Used to delete a specific timespan from the database /// </summary> /// <param name="id">The SpanID of the timespan to be deleted</param> /// <returns>Boolean indicating success or failure</returns> public bool DeleteTimeSpan(int id) { using (helpdesksystemContext context = new helpdesksystemContext()) { Timespans timespan = context.Timespans.FirstOrDefault(ts => ts.SpanId == id); if (timespan == null) { return(true); } context.Timespans.Remove(timespan); context.SaveChanges(); } return(true); }