示例#1
0
        public async Task <IActionResult> Update([FromBody] VmGoalMessage message)
        {
            var currentUser = await _currentUserService.GetCurrentUser(User);

            await _goalMessageService.Update(currentUser, message);

            return(NoContent());
        }
示例#2
0
        /// <inheritdoc />
        public async Task <VmGoalMessage> Create(
            ApplicationUser currentUser,
            VmGoalMessage message)
        {
            Goal goal = await _dataContext.Goals
                        .Include(x => x.Performer)
                        .Include(x => x.Observers)
                        .SingleOrDefaultAsync(x => x.Id == message.GoalId);

            if (goal == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest,
                                                $"The goal with id '{message.GoalId}' not found");
            }

            var model = _vmConverter.ToModel(message);

            model.Id           = 0;
            model.CreationDate = DateTime.Now.ToUniversalTime();

            var observers = goal.Observers
                            .Select(x => new UserGoalMessage
            {
                UserId      = x.ObserverId,
                User        = x.Observer,
                GoalMessage = model
            });

            model.MessageSubscribers = new List <UserGoalMessage>(observers)
            {
                new()
                {
                    UserId      = goal.PerformerId,
                    User        = goal.Performer,
                    GoalMessage = model
                }
            };

            await _dataContext.AddAsync(model);

            await _dataContext.SaveChangesAsync();

            _entityStateQueue.EnqueueId(currentUser.Id, model.Id,
                                        nameof(GoalMessage), EntityOperation.Create);

            return(_vmConverter.ToViewModel(model));
        }
示例#3
0
        /// <inheritdoc />
        public async Task Update(ApplicationUser currentUser, VmGoalMessage message)
        {
            var msg = await _dataContext.GoalMessages
                      .Where(x => x.OwnerId == currentUser.Id)
                      .SingleOrDefaultAsync(x => x.Id == message.Id);

            if (msg == null)
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest,
                                                $"Goal message with id = '{message.Id}' not found");
            }

            msg.Text         = message.Text;
            msg.LastEditDate = DateTime.Now.ToUniversalTime();

            _dataContext.Entry(msg).State = EntityState.Modified;
            await _dataContext.SaveChangesAsync();

            _entityStateQueue.EnqueueId(currentUser.Id, msg.Id,
                                        nameof(GoalMessage), EntityOperation.Update);
        }
示例#4
0
        public async Task <ActionResult <VmGoalMessage> > Create([FromBody] VmGoalMessage message)
        {
            var currentUser = await _currentUserService.GetCurrentUser(User);

            return(await _goalMessageService.Create(currentUser, message));
        }