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(); }
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}"); }
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(); }
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; } }
public async void ReturnArgumentOutOfRangeException_IfNoObject() { var command = new CreateActivityCommand(null); var handler = new CreateActivityHandler(_unitOfWork.Object); Assert.ThrowsAsync <ArgumentOutOfRangeException>(() => handler.Handle(command, new CancellationToken())); }
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}")); }
public async Task <IActionResult> Create(CreateActivityCommand command) { if (await Mediator.Send(command) != null) { return(RedirectToAction("Details", "Activities", new { id = command.Id })); } return(View(command)); }
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)); }
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)); } }
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}")); }
public IActionResult CreateActivity([FromBody] CreateActivityCommand newActivity) { try { _activityService.CreateActivity(newActivity); return(Ok()); } catch (ArgumentException ex) { return(BadRequest(ex)); } }
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)); }
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()); }
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); }
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)); }
public async Task <ActionResult> CreateActivityAsync([FromBody] CreateActivityCommand command) { return(StatusCode(200, await CommandAsync(command))); }
public async Task <Response <Activity> > Post([FromBody] CreateActivityCommand cmd) { return(await _mediator.Send(cmd)); }
public async Task <ActionResult <int> > CreateActivity([FromRoute] int contactId, [FromBody] CreateActivityCommand command) { command.ContactId = contactId; return(Ok(await Mediator.Send(command))); }
public SActivity MapCreateRequesttoActivity(CreateActivityCommand request) { return(_mapper.Map <CreateActivityCommand, SActivity>(request)); }
public WebApiResult <ActivityViewModel> Post([FromBody] CreateActivityCommand command) { var result = this._repository.ExecuteCommand(command); return(AutoMapper.Mapper.Map <CommandResult <Activity>, WebApiResult <ActivityViewModel> >(result)); }
public async Task <ActionResult <Unit> > Create(CreateActivityCommand command) { return(await Mediator.Send(command)); }