Example #1
0
        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));
        }
Example #2
0
        public async Task <ReplaceOneResult> UpdateEntityTechnology(string entityId, EntityTechnology entityTechnology)
        {
            var entity = await FindByIdAsync(entityId);

            var technologyList = entity.Technologies.ToList();
            var index          = technologyList.FindIndex(t => t.TechnologyId == entityTechnology.TechnologyId);

            entity.Technologies[index].UpdateEntityTechnology(entityTechnology);
            return(await ReplaceOneAsync(entityId, entity));
        }
Example #3
0
        public async Task <IActionResult> AddTechnology(
            [FromServices] IEntityRepository entityRepository,
            [FromServices] IQueue queue,
            [FromServices] ITechnologyRepository technologyRepository,
            [FromBody] EntityTechnology entityTechnology,
            string id)
        {
            try
            {
                var user = HttpContext.User;
                if (!_validation.Validate(entityTechnology))
                {
                    return(BadRequest());
                }
                await entityRepository.AddTechnology(entityTechnology, id, technologyRepository);

                var author = user.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;
                queue.PublishAsync("history", JsonConvert.SerializeObject(
                                       new HistoryMessage()
                {
                    Id   = entityTechnology.Id,
                    Data = JsonConvert.SerializeObject(
                        entityTechnology,
                        new JsonSerializerSettings
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    }
                        ),
                    Type   = "entity-technology",
                    Author = author
                }
                                       ));
                return(NoContent());
            }
            catch (ArgumentException ex)
            {
                return(BadRequest(new { message = ex.Message }));
            }
            catch (MongoWriteException exception)
            {
                if (exception.WriteError.Category == ServerErrorCategory.DuplicateKey)
                {
                    return(Conflict(new { message = "This technology is already in your entity" }));
                }
                return(BadRequest());
            }
        }
Example #4
0
        public async Task <IActionResult> UpdateTechnologyStatus(
            [FromServices] IEntityRepository entityRepository,
            [FromServices] IQueue queue,
            string id,
            [FromBody] EntityTechnology patchEntityTechnology)
        {
            var user       = HttpContext.User;
            var claims     = user.Claims;
            var userEntity = claims.FirstOrDefault(c => c.Type == ClaimTypes.PrimaryGroupSid).Value;
            var author     = user.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Name).Value;

            if ((user.IsInRole("admin") && userEntity != id) && !user.IsInRole("root"))
            {
                return(Unauthorized());
            }
            var entityTechnology =
                await entityRepository.FindTechnologyByIdAsync(id, patchEntityTechnology.TechnologyId);

            if (!_validation.Validate(entityTechnology))
            {
                return(BadRequest());
            }
            entityTechnology.UpdateEntityTechnology(patchEntityTechnology);
            var result = await entityRepository.UpdateEntityTechnology(id, entityTechnology);

            queue.PublishAsync("history", JsonConvert.SerializeObject(
                                   new HistoryMessage()
            {
                Id   = entityTechnology.Id,
                Data = JsonConvert.SerializeObject(
                    entityTechnology,
                    new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                }
                    ),
                Type   = "entity-technology",
                Author = author
            },
                                   new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            }
                                   ));
            return(NoContent());
        }
Example #5
0
 public void UpdateEntityTechnology(EntityTechnology entityTechnology)
 {
     Status = entityTechnology.Status;
     EntityTechnologyUrls = entityTechnology.EntityTechnologyUrls;
     Scope = entityTechnology.Scope;
 }