public async Task <IActionResult> PostAsync([FromBody] CreateActivityDto createActivityDto)
        {
            var user = await _context.Users.SingleOrDefaultAsync(x => x.UserName == _userAccessor.GetCurrentUserName());

            if (user == null)
            {
                return(NotFound(new { User = "******" }));
            }

            var activity = new Activity
            {
                Name      = createActivityDto.Name,
                AppUserId = user.Id,
                Status    = "Created",
                Result    = string.Empty
            };

            if (string.IsNullOrWhiteSpace(activity.Name))
            {
                activity.Name = "Default Activity";
            }

            activity.Name = string.Format("{0} by user {1} at {2}", activity.Name, user.UserName, DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffffffK"));


            await _context.Activities.AddAsync(activity);

            await _context.SaveChangesAsync();

            //_queue.QueueAsyncTask(() => PerformBackgroundJob(activity.Id));
            Task.Factory.StartNew(() => PerformBackgroundJob(activity.Id, user.UserName));

            return(CreatedAtRoute("ActivityByIdAsync", new { id = activity.Id }, _mapper.Map <Activity, ActivityDto>(activity)));
        }
Exemplo n.º 2
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());
        }
Exemplo n.º 3
0
        public async Task <IActionResult> CreateNewActivity([FromBody] CreateActivityDto createActivityDto)
        {
            var UsersId      = int.Parse(User.FindFirst("UsersId").Value);
            var UsersOrgId   = int.Parse(User.FindFirst("Organization").Value);
            var activityDate = new DateTime();

            if (createActivityDto.Time != null || createActivityDto.Date != null)
            {
                var splittedTime = createActivityDto.Time.Split(":");
                activityDate = DateTime.Parse(createActivityDto.Date).AddHours(double.Parse(splittedTime[0])).AddMinutes(double.Parse(splittedTime[1]));
            }

            var newActivity = new Activity()
            {
                Title          = createActivityDto.Title,
                Description    = createActivityDto.Description,
                Date           = activityDate,
                Type           = createActivityDto.Type,
                CreatorId      = UsersId,
                Archived       = false,
                OrganizationId = UsersOrgId
            };

            if (createActivityDto.CustomerId != 0 || createActivityDto.CustomerId != null)
            {
                newActivity.CustomerId = createActivityDto.CustomerId;
            }

            await _context.Activities.AddAsync(newActivity);

            await _context.SaveChangesAsync();

            // Check if users or contactpersons are null

            if (createActivityDto.UsersForActivity != null)
            {
                foreach (var user in createActivityDto.UsersForActivity)
                {
                    var activityUser = new ActivityUser()
                    {
                        UserId     = user.Id,
                        ActivityId = newActivity.Id
                    };
                    await _context.ActivityUsers.AddAsync(activityUser);
                }
            }

            if (createActivityDto.ContactpersonsForActivity != null)
            {
                foreach (var contactperson in createActivityDto.ContactpersonsForActivity)
                {
                    var activityContactperson = new ActivityContactperson()
                    {
                        ContactpersonId = contactperson.Id,
                        ActivityId      = newActivity.Id
                    };
                    await _context.ActivityContactpersons.AddAsync(activityContactperson);
                }
            }


            await _context.SaveChangesAsync();

            return(Ok(createActivityDto));
        }