public async Task <IActionResult> PatchTechnology( [FromServices] ITechnologyRepository technologyRepository, [FromServices] IQueue queue, string id, [FromBody] JsonPatchDocument <Technology> technologyPatch) { var user = HttpContext.User; if (!user.IsInRole("root")) { return(Unauthorized()); } var technology = await technologyRepository.FindByIdAsync(id); if (technology == null) { return(BadRequest()); } technologyPatch.ApplyTo(technology); if (!_validation.Validate(technology)) { return(BadRequest()); } await technologyRepository.ReplaceOneAsync(id, technology); var author = user.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value; queue.PublishAsync("history", JsonConvert.SerializeObject( new HistoryMessage() { Id = id, Data = JsonConvert.SerializeObject(technology), Type = "technology", Author = author } )); return(NoContent()); }
public async Task <UpdateResult> AddTechnology(EntityTechnology entityTechnology, string id, ITechnologyRepository technologyRepository) { var entity = await FindByIdAsync(id); var technology = await technologyRepository.FindByIdAsync(entityTechnology.TechnologyId); if (technology == null) { throw new ArgumentException("Invalid technology id"); } var technologies = entity.Technologies; if (technologies != null && technologies.Length > 0) { if (technologies.Any(t => t.TechnologyId == entityTechnology.TechnologyId)) { throw new ArgumentException("This technology is already in your entity"); } Array.Resize(ref technologies, technologies.Length + 1); technologies[technologies.GetUpperBound(0)] = entityTechnology; } else { technologies = new EntityTechnology[1]; technologies[0] = entityTechnology; } return(await UpdateOneAsync(id, "technologies", technologies)); }
public async Task <EntityTechnology> PopulateEntityTechnologies(string entityId, string technologyId, ITechnologyRepository technologyRepository) { var newEntityTechnology = await FindTechnologyByIdAsync(entityId, technologyId); newEntityTechnology.Technology = await technologyRepository.FindByIdAsync(technologyId); return(newEntityTechnology); }
public async Task <IActionResult> GetTechnologyById([FromServices] ITechnologyRepository technologyRepository, string id) { var result = await technologyRepository.FindByIdAsync(id); if (result != null) { return(Ok(result)); } return(NoContent()); }