Exemplo n.º 1
0
        public async Task <IActionResult> Upsert([FromBody] TeachingEvent teachingEvent)
        {
            var operation = new TeachingEventUpsertOperation(teachingEvent);
            var validator = new TeachingEventUpsertOperationValidator(_crm);
            var result    = validator.Validate(operation);

            result.AddToModelState(ModelState, null);

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            // Save independently so that the building gets an Id populated immediately.
            // We also persist in the cache so it is immediately available.
            if (teachingEvent.Building != null)
            {
                _crm.Save(teachingEvent.Building);
                await _store.SaveAsync(new TeachingEventBuilding[] { teachingEvent.Building });

                teachingEvent.BuildingId = teachingEvent.Building.Id;
            }

            _crm.Save(teachingEvent);
            await _store.SaveAsync(new TeachingEvent[] { teachingEvent });

            return(CreatedAtAction(
                       actionName: nameof(Get),
                       routeValues: new { readableId = teachingEvent.ReadableId },
                       value: teachingEvent));
        }
        public void Constructor_WithTeachingEvent()
        {
            var teachingEvent = new TeachingEvent()
            {
                Id = Guid.NewGuid(), ReadableId = "test-1"
            };

            var operation = new TeachingEventUpsertOperation(teachingEvent);

            operation.Id.Should().Be(teachingEvent.Id);
            operation.ReadableId.Should().Be(teachingEvent.ReadableId);
        }
        public void Validate_WhenExistingEventWithReadableIdIsNotFound_HasNoErrors()
        {
            var operation = new TeachingEventUpsertOperation()
            {
                ReadableId = "new"
            };

            _mockCrm.Setup(m => m.GetTeachingEvent("new")).Returns <TeachingEvent>(null);

            var result = _validator.Validate(operation);

            result.IsValid.Should().BeTrue();
        }
        public void Validate_WhenExistingEventWithReadableIdHasDifferentTeachingEventId_HasErrors()
        {
            var operation = new TeachingEventUpsertOperation()
            {
                Id = Guid.NewGuid(), ReadableId = "existing"
            };
            var teachingEvent = new TeachingEvent()
            {
                Id = Guid.NewGuid()
            };

            _mockCrm.Setup(m => m.GetTeachingEvent("existing")).Returns(teachingEvent);

            var result = _validator.Validate(operation);

            result.IsValid.Should().BeFalse();
        }