/// <summary> /// Adds a test unit to the database. /// </summary> /// <param name="unitID">The ID of the unit to update. Should not be provided if adding a new helpdesk.</param> /// <param name="helpdeskID">The ID of the helpdesk that the unit is being added to.</param> /// <param name="name">Name of the unit (auto-generates if not provided, or empty string is provided).</param> /// <param name="code">Code of the unit (auto-generates if not provided, or empty string is provided).</param> /// <param name="isDeleted">Whether or not the helpdesk is removed from the system.</param> /// <param name="topics">A list of topics associated with the unit.</param> /// <returns></returns> public TestDataUnit AddUpdateUnit(int unitID = 0, int?helpdeskID = null, string name = "", string code = "", bool?isDeleted = false, List <string> topics = null) { var request = new AddUpdateUnitRequest(); if (helpdeskID != null) { request.HelpdeskID = (int)helpdeskID; } if (name != null) { if (name == "" && PopulateEmptyStrings) { request.Name = AlphaNumericStringGenerator.GetString(10); } else { request.Name = name; } } if (code != null) { if (code == "" && PopulateEmptyStrings) { request.Code = AlphaNumericStringGenerator.GetString(8); } else { request.Code = code; } } if (isDeleted != null) { request.IsDeleted = (bool)isDeleted; } if (topics != null) { request.Topics = topics; } var facade = new UnitsFacade(); var response = facade.AddOrUpdateUnit(unitID, request); TestDataUnit data = new TestDataUnit(request, response); return(data); }
public IActionResult AddOrUpdateUnit([FromRoute] int id, [FromBody] AddUpdateUnitRequest request) { if (request == null) { return(BadRequest()); } if (!IsAuthorized()) { return(Unauthorized()); } try { var facade = new UnitsFacade(); var response = facade.AddOrUpdateUnit(id, request); switch (response.Status) { case HttpStatusCode.OK: return(Ok(response)); case HttpStatusCode.BadRequest: return(BadRequest(BuildBadRequestMessage(response))); case HttpStatusCode.NotFound: return(NotFound()); case HttpStatusCode.InternalServerError: return(StatusCode(StatusCodes.Status500InternalServerError)); } s_logger.Fatal("This code should be unreachable, unknown result has occured."); } catch (Exception ex) { s_logger.Error(ex, "Unable to add or update unit."); } return(StatusCode(StatusCodes.Status500InternalServerError)); }
/// <summary> /// Adds a unit to the database using provided unit request. /// </summary> /// <param name="request">The request containing the information to add a unit.</param> /// <returns>The id of the unit.</returns> public int?AddUnit(AddUpdateUnitRequest request) { int?unitId = null; using (helpdesksystemContext context = new helpdesksystemContext()) { using (var trans = context.Database.BeginTransaction()) { try { Unit newUnit = new Unit() { Code = request.Code, IsDeleted = request.IsDeleted, Name = request.Name }; context.Add(newUnit); context.SaveChanges(); unitId = newUnit.UnitId; if (!unitId.HasValue || unitId.Value == 0) { trans.Rollback(); throw new Exception("Unable to add unit"); } Topic otherTopicOption = new Topic() { UnitId = newUnit.UnitId, Name = "Other", IsDeleted = false }; context.Topic.Add(otherTopicOption); foreach (string topic in request.Topics) { context.Topic.Add(new Topic() { Name = topic, UnitId = unitId.Value, IsDeleted = false, }); } context.Helpdeskunit.Add(new Helpdeskunit() { HelpdeskId = request.HelpdeskID, UnitId = unitId.Value }); context.SaveChanges(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } } return(unitId); }
/// <summary> /// Updates a specific unit /// </summary> /// <param name="id">ID of the unit to be deleted</param> /// <returns>Indication of the result of the operation</returns> public bool UpdateUnit(int id, AddUpdateUnitRequest request) { using (helpdesksystemContext context = new helpdesksystemContext()) { using (var trans = context.Database.BeginTransaction()) { try { var unit = context.Unit.Include("Topic").FirstOrDefault(u => u.UnitId == id); if (unit == null) { throw new NotFoundException("Unable to find unit!"); } unit.Name = request.Name; unit.IsDeleted = request.IsDeleted; unit.Code = request.Code; foreach (Topic topic in unit.Topic) { if (!request.Topics.Contains(topic.Name)) { topic.IsDeleted = true; } } foreach (string topic in request.Topics) { var existingTopic = unit.Topic.FirstOrDefault(t => t.Name == topic); if (existingTopic != null) { if (existingTopic.IsDeleted) { existingTopic.IsDeleted = false; } } else { unit.Topic.Add(new Topic() { IsDeleted = false, Name = topic, UnitId = unit.UnitId }); } } context.SaveChanges(); trans.Commit(); } catch (Exception ex) { trans.Rollback(); throw ex; } } } return(true); }
public TestDataUnit(AddUpdateUnitRequest request, AddUpdateUnitResponse response) { Request = request; Response = response; }