コード例 #1
0
        public IActionResult Create()
        {
            EventFormModel eventForm = new EventFormModel
            {
                Categories         = categoryService.UserCategories(this.User.GetUserId()),
                SelectedCategories = new List <string>()
            };

            return(View(eventForm));
        }
コード例 #2
0
        public async Task <ActionResult <Event> > CreateEvent([FromBody] EventFormModel newEvent)
        {
            if (!ModelState.IsValid)
            {
                return(this.BadRequest());
            }

            Event eventToCreate = this.mapper.Map <Event>(newEvent);
            await eventService.CreateEventAsync(eventToCreate);

            return(CreatedAtAction(nameof(GetById), new { id = eventToCreate.Id }, eventToCreate));
        }
コード例 #3
0
        public async Task CreateEvent_WhenModelIsNotValid_ReturnsBadRequest()
        {
            // Arrange
            EventFormModel fakeDataFromBody = new EventFormModel();

            this.controller.ModelState.AddModelError("StartDate", "StartDate must be in the future.");

            // Act
            ActionResult <Event> result = await this.controller.CreateEvent(fakeDataFromBody);

            // Assert
            Assert.IsType <BadRequestResult>(result.Result);
        }
コード例 #4
0
        public IActionResult Create(EventFormModel model)
        {
            this.eventService.Create(
                model.ImageUrl,
                model.Title,
                model.Location,
                model.Description,
                model.DateStarts,
                model.DateEnds,
                this.User.GetUserId());

            return(RedirectToAction("Index", "Users"));
        }
コード例 #5
0
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            EventFormModel model = (EventFormModel)validationContext.ObjectInstance;

            if (model.Start > model.End)
            {
                return(new ValidationResult
                           ("The End Date must be greater than the Start Date."));
            }
            else
            {
                return(ValidationResult.Success);
            }
        }
コード例 #6
0
        public IActionResult CreateForm([FromBody] EventFormModel model)
        {
            var formEvent = _mapper.Map <EventForm>(model);

            try
            {
                _formEventService.CreateFormEvent(formEvent, model.kindEvent);
                return(Ok());
            }
            catch (AppException ex)
            {
                return(BadRequest(new { meessage = ex.Message.ToString() }));
            }
        }
コード例 #7
0
        public async Task UpdateEvent_WhenNoExistingEntityWithId_ReturnsNotFound()
        {
            // Arrange
            EventFormModel fakeDataFromBody = new EventFormModel();
            int            fakeId           = 1;

            mockService.Setup(service => service.GetByIdAsync(fakeId))
            .ReturnsAsync((Event)null);

            // Act
            ActionResult <Event> result = await this.controller.UpdateEvent(fakeId, fakeDataFromBody);

            // Assert
            Assert.IsType <NotFoundResult>(result.Result);
        }
コード例 #8
0
        public async Task <IActionResult> Edit(int event_id, EventFormModel model)
        {
            EventDTO eventDTO = new EventDTO
            {
                Title       = model.Title,
                Description = model.Description,
                DateStarts  = model.DateStarts,
                DateEnds    = model.DateEnds,
                Location    = model.Location,
                Categories  = model.SelectedCategories,
                ImUrl       = model.ImUrl,
            };
            await eventService.Edit(event_id, eventDTO);

            return(RedirectToAction("Index", "Profile"));
        }
コード例 #9
0
        public IActionResult Edit(int event_id)
        {
            var eventInfo      = this.eventService.EventById(event_id);
            var eventFormModel = new EventFormModel
            {
                Title              = eventInfo.Title,
                Location           = eventInfo.Location,
                Description        = eventInfo.Description,
                DateEnds           = eventInfo.DateEnds,
                DateStarts         = eventInfo.DateStarts,
                ImUrl              = eventInfo.ImUrl,
                Categories         = categoryService.UserCategories(this.User.GetUserId()),
                SelectedCategories = new List <string>()
            };

            return(this.ViewOrNotFound(eventFormModel));
        }
コード例 #10
0
        public IActionResult Edit(EventFormModel model)
        {
            var filePath = string.Format(CallendarJasonDbPath, Directory.GetCurrentDirectory());

            model.date = NewFormatDate(model.date);

            if (!ObjToJsonConverterExtensions.EditEventInJsonFile(filePath, model))
            {
                this.TempData.AddWarningMessage(string.Format(TempDataEditFailText, ModelName, EndingLetterO));

                return(this.RedirectToAction(nameof(this.Edit)));
            }

            this.TempData.AddSuccessMessage(string.Format(TempDataEditCommentText, ModelName, EndingLetterO));

            return(this.RedirectToAction(nameof(this.Events)));
        }
コード例 #11
0
        public async Task <IActionResult> Edit(long id, EventFormModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await this.userManager.FindByNameAsync(this.HttpContext.User.Identity.Name);

            var successEdited = await this.eventService.Edit(id, model.Name, model.Location, model.Start, model.End, user.Id);

            if (!successEdited)
            {
                return(BadRequest());
            }

            return(RedirectToAction(nameof(EventsByUser)));
        }
コード例 #12
0
        public async Task <IActionResult> Create(EventFormModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await this.userManager.FindByNameAsync(this.HttpContext.User.Identity.Name);


            var successCreated = await this.eventService.Create(model.Name, model.Location, model.Start, model.End, user.Id);

            if (!successCreated)
            {
                return(BadRequest());
            }

            return(RedirectToAction("Index", "Home"));
        }
コード例 #13
0
        public async Task <ActionResult <Event> > UpdateEvent(int id, [FromBody] EventFormModel eventToUpdate)
        {
            if (!ModelState.IsValid)
            {
                return(this.BadRequest());
            }

            Event eventFromService = await this.eventService.GetByIdAsync(id);

            if (eventFromService == null)
            {
                return(this.NotFound());
            }

            this.mapper.Map(eventToUpdate, eventFromService);
            await eventService.UpdateEventAsync(eventFromService);

            return(Ok(eventFromService));
        }
コード例 #14
0
        public async Task <IActionResult> Create(EventFormModel model)
        {
            EventDTO eventDTO = new EventDTO {
                Title       = model.Title,
                Location    = model.Location,
                Description = model.Description,
                DateStarts  = model.DateStarts,
                DateEnds    = model.DateEnds,
                CreatorId   = this.User.GetUserId(),
                Categories  = model.SelectedCategories,
                ImUrl       = model.ImUrl,
            };

            await eventService.Create(
                eventDTO
                );

            return(RedirectToAction("Index", "Profile"));
        }
コード例 #15
0
        public IActionResult Create(EventFormModel model)
        {
            model.title       = this.html.Sanitize(model.title);
            model.description = this.html.Sanitize(model.description);
            model.id          = Guid.NewGuid().ToString();
            model.date        = NewFormatDate(model.date);

            var filePath = string.Format(CallendarJasonDbPath, Directory.GetCurrentDirectory());

            if (!ObjToJsonConverterExtensions.CreateEventInJsonFile(model, filePath))
            {
                this.TempData.AddWarningMessage(string.Format(TempDataCreateFailText, ModelName, EndingLetterO));

                return(this.RedirectToAction(nameof(this.Create)));
            }

            this.TempData.AddSuccessMessage(string.Format(TempDataCreateCommentText, ModelName, EndingLetterO));

            return(this.RedirectToAction(nameof(this.Events)));
        }
コード例 #16
0
        public async Task <IActionResult> Edit(long id)
        {
            var eventItem = await this.eventService.FindById(id);


            if (eventItem == null)
            {
                return(BadRequest());
            }

            var model = new EventFormModel
            {
                Name     = eventItem.Name,
                Location = eventItem.Location,
                Start    = eventItem.Start,
                End      = eventItem.End
            };

            return(View(model));
        }
コード例 #17
0
        public async Task UpdateEvent_ReturnsOkWithModel()
        {
            // Arrange
            int            fakeId              = 1;
            EventFormModel fakeDataFromBody    = new EventFormModel();
            Event          fakeDataFromService = new Event();

            mockService.Setup(service => service.GetByIdAsync(fakeId))
            .ReturnsAsync(fakeDataFromService);
            mockMapper.Setup(mapper => mapper.Map(fakeDataFromBody, fakeDataFromService));
            mockService.Setup(service => service.UpdateEventAsync(fakeDataFromService));
            fakeDataFromService.Name = "Test";

            // Act
            ActionResult <Event> result = await this.controller.UpdateEvent(fakeId, fakeDataFromBody);

            // Assert
            OkObjectResult okObjectResult = Assert.IsType <OkObjectResult>(result.Result);
            Event          returnValue    = Assert.IsType <Event>(okObjectResult.Value);

            Assert.Equal("Test", returnValue.Name);
        }
コード例 #18
0
        public async Task CreateEvents_ReturnsCreateAtActionWithModel()
        {
            // Arrange
            EventFormModel fakeDataFromBody       = new EventFormModel();
            Event          fakeMappedDataFromBody = new Event();

            fakeMappedDataFromBody.Name = "Test";
            mockMapper.Setup(mapper => mapper.Map <Event>(fakeDataFromBody))
            .Returns(fakeMappedDataFromBody);
            mockService.Setup(service => service.CreateEventAsync(fakeMappedDataFromBody));

            // Act
            ActionResult <Event> result = await this.controller.CreateEvent(fakeDataFromBody);

            // Assert
            CreatedAtActionResult createdAtActionResult = Assert.IsType <CreatedAtActionResult>(result.Result);

            Assert.Equal("GetById", createdAtActionResult.ActionName);
            Assert.True(createdAtActionResult.RouteValues.ContainsKey("id"));

            Event returnValue = Assert.IsType <Event>(createdAtActionResult.Value);

            Assert.Equal("Test", returnValue.Name);
        }