Exemple #1
0
        /// <exception cref="Bad">
        /// <see cref="Bad.AccessError"/> if sender if not client/respondent/admin.
        /// </exception>
        /// <exception cref="Bad">
        /// <see cref="Bad.UselessRequest"/> if appeal is resolved.
        /// </exception>
        /// <exception cref="Bad">
        /// <see cref="Bad.UnknownError"/>.
        /// </exception>
        public async Task <AppealMessage> HandleAsync(CreateAppealMessageData data)
        {
            _data = data.Validate();

            await _data.Sender.LoadRoleSetAsync(_userManager);

            CheckAccess();
            CheckAppeal();

            _data.EnsureNoErrors();

            var newMessage = new AppealMessage
            {
                Appeal  = _data.Appeal,
                Sender  = _data.Sender,
                Content = _data.Content,
            };

            try
            {
                await Db.AddAsync(newMessage);

                await Db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Cannot create new AppealMessage!");
                throw Bad.UnknownError;
            }

            OnCreated?.Invoke(newMessage);

            return(newMessage);
        }
Exemple #2
0
        /// <exception cref="Bad">
        /// <see cref="Bad.DataError"/>.
        /// </exception>
        /// <exception cref="Bad">
        /// <see cref="Bad.UnknownError"/>.
        /// </exception>
        public async Task <Appeal> CreateAsync(CreateAppealData data)
        {
            _data = await data.ValidateAsync(Db);

            _data.EnsureNoErrors();

            CheckTopic();

            _data.EnsureNoErrors();

            var newAppeal = new Appeal
            {
                TopicId = data.TopicId,
                Client  = data.Client
            };

            var firstMessageData = new CreateAppealMessageData
            {
                Content = data.FirstMessage,
                Appeal  = newAppeal,
                Sender  = newAppeal.Client
            }.Validate();

            firstMessageData.EnsureNoErrors();

            var firstMessage = new AppealMessage
            {
                Appeal  = firstMessageData.Appeal,
                Sender  = firstMessageData.Sender,
                Content = firstMessageData.Content,
            };

            await Db.AddAsync(newAppeal);

            await Db.AddAsync(firstMessage);

            try
            {
                await Db.SaveChangesAsync();
            }
            catch (Exception ex)
            {
                Logger.LogError(ex, "Can't create new Appeal with first AppealMessage!");
                throw Bad.UnknownError;
            }

            return(newAppeal);
        }