Example #1
0
        public async Task <OknObject> CreateObjectEvent(CreateObjectEventCommand command, CancellationToken cancellationToken)
        {
            var filter = Builders <ObjectEntity> .Filter.Where(x => x.ObjectId == command.ObjectId);

            var originalEntity = await _context.Objects
                                 .Find(filter)
                                 .FirstOrDefaultAsync(cancellationToken);

            if (originalEntity == null)
            {
                throw new ObjectEventNotExistException("Object event with this id doesn't exist");
            }

            SetObjectVersionIfNotExist(command, originalEntity);

            var eventEntity = new ObjectEventEntity
            {
                EventId     = command.EventId,
                Name        = command.Name,
                Description = command.Description,
                OccuredAt   = command.OccuredAt,
                Type        = command.Type,
                Author      = new UserInfoEntity(command.UserId, command.UserName, command.Email)
            };

            if (command.Photos != null && command.Photos.Any())
            {
                eventEntity.Photos = command.Photos.Select(x => ProcessFileInfo(x)).ToList();
            }

            if (command.Files != null && command.Files.Any())
            {
                eventEntity.Files = command.Files.Select(x => ProcessFileInfo(x)).ToList();
            }

            originalEntity.AddEvent(eventEntity);

            await IncObjectVersion(command, originalEntity, originalEntity, cancellationToken);

            var result = await _context.Objects.ReplaceOneAsync(filter, originalEntity, cancellationToken : cancellationToken);

            return(null);
        }
        public async Task <IActionResult> Create([FromRoute] string objectId,
                                                 [FromBody] CreateObjectEventViewModel request)
        {
            if (request == null)
            {
                return(BadRequest());
            }

            var currentUser = HttpContext.User;

            var updateCommand = new CreateObjectEventCommand(objectId, Guid.NewGuid().ToString())
            {
                Name        = request.Name,
                Description = request.Description,
                OccuredAt   = request.OccuredAt ?? DateTime.UtcNow,
                Files       = request.Files?.Select(x => new FileInfo
                {
                    FileId      = x.FileId,
                    Description = x.Description
                }).ToList(),
                Photos = request.Photos?.Select(x => new FileInfo
                {
                    FileId      = x.FileId,
                    Description = x.Description
                }).ToList()
            };

            updateCommand.SetCreator(
                long.Parse(currentUser.FindFirstValue(ClaimTypes.NameIdentifier)).ToString(),
                currentUser.FindFirstValue(ClaimTypes.Name),
                currentUser.FindFirstValue(ClaimTypes.Email));

            await _objectsEventsRepository.CreateObjectEvent(updateCommand, CancellationToken.None);

            return(Ok());
        }