Пример #1
0
        public void CreateActivity(CreateActivityCommand newActivity, int idUser)
        {
            var creator = db.Users.FirstOrDefault(u => u.Id == idUser);

            var activity = new Activity
            {
                Creator     = creator,
                Name        = newActivity.Name,
                Description = newActivity.Description,
                Capacity    = newActivity.Capacity
            };

            foreach (int id in newActivity.TagIds)
            {
                var tag = db.Tags.Find(id);
                if (tag == null)
                {
                    throw new ArgumentException($"Tag with id {id} has not been found");
                }

                activity.Tags.Add(tag);
            }

            db.Activities.Add(activity);
            db.SaveChanges();
        }
Пример #2
0
        public async Task Activities_controller_post_should_return_accepted()
        {
            var busClientMock        = new Mock <IBusClient>();
            var activityServiceMock  = new Mock <IActivityService>();
            var activitiesController = new ActivitiesController(busClientMock.Object, activityServiceMock.Object);
            var userId = Guid.NewGuid();

            activitiesController.ControllerContext = new ControllerContext
            {
                HttpContext = new DefaultHttpContext
                {
                    User = new ClaimsPrincipal(new ClaimsIdentity(
                                                   new Claim[] { new Claim(ClaimTypes.Name, userId.ToString()) }, "test"))
                }
            };

            var command = new CreateActivityCommand
            {
                Id     = Guid.NewGuid(),
                UserId = userId
            };

            var result = await activitiesController.CreateActivity(command);

            var contentResult = result as AcceptedResult;

            contentResult.Should().NotBeNull();
            contentResult.Location.Should().BeEquivalentTo($"Activities/{command.Id}");
        }
Пример #3
0
        public void CreateActivity(CreateActivityCommand newActivity)
        {
            var creator = db.Users.Find(newActivity.CreatorId)
                          ?? throw new ArgumentException($"{nameof(newActivity.CreatorId)} has to point to an existed user");

            var activity = new Activity
            {
                Creator     = creator,
                Name        = newActivity.Name,
                Description = newActivity.Description,
                Capacity    = newActivity.Capacity
            };

            foreach (int id in newActivity.TagIds)
            {
                var tag = db.Tags.Find(id);
                if (tag == null)
                {
                    throw new ArgumentException($"Tag with id {id} has not been found");
                }

                activity.Tags.Add(tag);
            }

            db.Activities.Add(activity);
            db.SaveChanges();
        }
Пример #4
0
        public async Task <IActionResult> CreateActivity([FromBody] ActivityModel model)
        {
            if (!ModelState.IsValid)
            {
                return(new BadRequestObjectResult("Invalid model inputs"));
            }

            CreateActivityCommand command = new CreateActivityCommand(model);

            try
            {
                var result = this._mediator.Send(command).Result;

                if (result == null)
                {
                    return(new BadRequestObjectResult("Something went wrong"));
                }
                if (result.GetType() == typeof(bool) && (bool)result == false)
                {
                    return(new BadRequestObjectResult("Something went wrong"));
                }
                if (result.GetType() == typeof(string))
                {
                    return(new BadRequestObjectResult(result));
                }
                return(new OkObjectResult(result));
            }
            catch (Exception e)
            {
                throw;
            }
        }
Пример #5
0
        public async void ReturnArgumentOutOfRangeException_IfNoObject()
        {
            var command = new CreateActivityCommand(null);
            var handler = new CreateActivityHandler(_unitOfWork.Object);

            Assert.ThrowsAsync <ArgumentOutOfRangeException>(() => handler.Handle(command, new CancellationToken()));
        }
Пример #6
0
        public async Task <IActionResult> Post([FromBody] CreateActivityCommand command)
        {
            command.Id        = Guid.NewGuid().ToString();
            command.CreatedAt = DateTime.UtcNow;
            await _busClient.PublishAsync(command);

            return(Accepted($"activities/{command.Id}"));
        }
Пример #7
0
        public async Task <IActionResult> Create(CreateActivityCommand command)
        {
            if (await Mediator.Send(command) != null)
            {
                return(RedirectToAction("Details", "Activities", new { id = command.Id }));
            }


            return(View(command));
        }
Пример #8
0
        public IActionResult CreateActivity([FromBody] CreateActivityCommand command)
        {
            var exception = _mediator.Send(command).Exception;

            if (exception != null)
            {
                throw exception.InnerException;
            }

            return(Ok(command));
        }
        public async Task <IActionResult> Post(CreateActivityCommand activityCommand)
        {
            var activity = await this.Mediator.Send(activityCommand);

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

            return(Ok(activity));
        }
Пример #10
0
 public IActionResult CreateActivity([FromBody] CreateActivityCommand newActivity)
 {
     try
     {
         _activityService.CreateActivity(newActivity, UserId);
         return(Ok(new { result = "Activity successfully created." }));
     }
     catch (ArgumentException ex)
     {
         return(BadRequest(ex.Message));
     }
 }
Пример #11
0
        public async Task <ActionResult> CreateActivity([FromBody] CreateActivityCommand command)
        {
            var id        = Guid.NewGuid();
            var createdAt = DateTime.UtcNow;

            command.Id        = id;
            command.CreatedAt = createdAt;
            command.UserId    = Guid.Parse(User.Identity.Name);
            await busClient.PublishAsync(command);

            return(Accepted($"Activities/{command.Id}"));
        }
Пример #12
0
 public IActionResult CreateActivity([FromBody] CreateActivityCommand newActivity)
 {
     try
     {
         _activityService.CreateActivity(newActivity);
         return(Ok());
     }
     catch (ArgumentException ex)
     {
         return(BadRequest(ex));
     }
 }
Пример #13
0
        public IActionResult CreateByStatus(Guid projectId, ActivityStatus status)
        {
            var model = new CreateActivityCommand {
                Status = status
            };

            ViewBag.ApplicationUsers = Mediator.Send(new GetProjectMembersDtoQuery {
                Id = projectId
            }).Result;
            ViewBag.ActivityTypes = GetEnabledProjectActivityTypeSelectList(projectId);
            ViewBag.ActivityLists = Mediator.Send(new GetActivityListsDtoQuery {
                ProjectId = projectId
            }).Result;
            ViewBag.Sprints = Mediator.Send(new GetSprintsDtoQuery {
                ProjectId = projectId
            }).Result;

            return(PartialView("_CreateByStatus", model));
        }
Пример #14
0
        public async Task <IActionResult> CreateAsync([FromBody] CreateActivityDto dto, [FromHeader(Name = "x-requestid")] string requestId)
        {
            if (Guid.TryParse(requestId, out Guid guid) && guid != Guid.Empty)
            {
                var userId = _identityService.GetUserIdentity();
                // TODO 获取用户信息
                var creator = new Attendee(userId, "nickname", "", 1, true);
                var address = new Address("杭州", "西湖区", "西湖风景区", 0, 0);
                var command = new CreateActivityCommand(creator, dto.Title, dto.Content, dto.EndRegisterTime, dto.ActivityStartTime, dto.ActivityEndTime, address, dto.CategoryId, dto.AddressVisibleRuleId, dto.LimitsNum);
                var requestCreateActivity = new IdentifiedCommand <CreateActivityCommand, int>(command, guid);
                var activityId            = await _mediator.Send(requestCreateActivity);

                if (activityId > 0)
                {
                    return(Created(Url.Action(nameof(ActivitiesController.Get), new { activityId }), activityId));
                }
            }

            return(BadRequest());
        }
Пример #15
0
        public async void ReturnNotNullActivity()
        {
            var tempActivity = new Activity()
            {
                ActivityName = "test", Id = 1
            };
            var tempActivityModel = new ActivityModel()
            {
                ActivityName = "test",
                ActivityId   = 1
            };

            _unitOfWork.Setup(mock => mock.ActivityRepository.Create(It.IsAny <Activity>()))
            .Returns(tempActivity);

            var command     = new CreateActivityCommand(tempActivityModel);
            var handler     = new CreateActivityHandler(_unitOfWork.Object);
            var returnValue = await handler.Handle(command, new CancellationToken());

            Assert.NotNull(returnValue);
        }
Пример #16
0
            public async Task <Result <Unit> > Handle(CreateActivityCommand request, CancellationToken cancellationToken)
            {
                var user = await _context.Users.FirstOrDefaultAsync(x => x.UserName == _userAccessor.GetUsername());

                var attendee = new ActivityAttendee {
                    AppUser  = user,
                    Activity = request.Activity,
                    IsHost   = true
                };

                request.Activity.Attendees.Add(attendee);

                _context.Activities.Add(request.Activity);

                var result = await _context.SaveChangesAsync() > 0;

                if (!result)
                {
                    return(Result <Unit> .Failure("Failed to create activity"));
                }

                return(Result <Unit> .Success(Unit.Value));
            }
Пример #17
0
 public async Task <ActionResult> CreateActivityAsync([FromBody] CreateActivityCommand command)
 {
     return(StatusCode(200, await CommandAsync(command)));
 }
Пример #18
0
 public async Task <Response <Activity> > Post([FromBody] CreateActivityCommand cmd)
 {
     return(await _mediator.Send(cmd));
 }
Пример #19
0
 public async Task <ActionResult <int> > CreateActivity([FromRoute] int contactId, [FromBody] CreateActivityCommand command)
 {
     command.ContactId = contactId;
     return(Ok(await Mediator.Send(command)));
 }
Пример #20
0
 public SActivity MapCreateRequesttoActivity(CreateActivityCommand request)
 {
     return(_mapper.Map <CreateActivityCommand, SActivity>(request));
 }
Пример #21
0
        public WebApiResult <ActivityViewModel> Post([FromBody] CreateActivityCommand command)
        {
            var result = this._repository.ExecuteCommand(command);

            return(AutoMapper.Mapper.Map <CommandResult <Activity>, WebApiResult <ActivityViewModel> >(result));
        }
Пример #22
0
 public async Task <ActionResult <Unit> > Create(CreateActivityCommand command)
 {
     return(await Mediator.Send(command));
 }