Exemplo n.º 1
0
        public async Task <IActionResult> PostTask([FromBody] TaskIM task)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var model = _mapper.Map <SIGOA.Data.Task>(task);


            foreach (var item in task.Badges.Where(d => d.Selected))
            {
                model.TaskBadges.Add(new TaskBadge {
                    TaskId = model.Id, BadgeId = int.Parse(item.Value)
                });
            }

            _context.Tasks.Add(model);
            await _context.SaveChangesAsync();

            var user = await _context.Users.FindAsync(model.Performer);

            if (Infrastructure.Helpers.Common.IsValidEmail(user.Email))
            {
                var emailAccount = await _context.EmailAccounts.FirstOrDefaultAsync(d => d.IsDefault);

                if (emailAccount != null)
                {
                    if (!string.IsNullOrEmpty(emailAccount.Password))
                    {
                        var pw = EncryptionHelper.Decrypt(emailAccount.Password);
                        emailAccount.Password = pw;
                    }
                    var subject = $"新任务:{model.Title}";
                    var body    = $"{model.Body}<p><a href='http://oa.heiniaozhi.cn/myzone/mytasks/detail/{model.Id}'>去网站查看</a></p>";


                    BackgroundJob.Enqueue(() => _emailService.SendMail("TZGOA", emailAccount.Email, user.Email, string.Empty,
                                                                       subject, body, emailAccount.Smtpserver, emailAccount.Email, string.Empty, emailAccount.UserName,
                                                                       emailAccount.Password, emailAccount.Port, emailAccount.EnableSsl));
                }
            }

            return(Ok("已成功创建"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> PutTask([FromRoute] int id, [FromBody] TaskIM task)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != task.Id)
            {
                return(BadRequest());
            }

            var model = await _context.Tasks.Include(d => d.TaskBadges).FirstOrDefaultAsync(d => d.Id == id);

            model = _mapper.Map(task, model);
            model.TaskBadges.Clear();
            _context.Entry(model).State = EntityState.Modified;

            foreach (var item in task.Badges.Where(d => d.Selected))
            {
                _context.TaskBadges.Add(new TaskBadge {
                    TaskId = model.Id, BadgeId = int.Parse(item.Value)
                });
            }


            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TaskExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok("已成功更新"));
        }