Пример #1
0
        public async Task Add()
        {
            var userID = Guid.NewGuid();
            await _unitOfWork.SaveAsync();

            var listModel = new ToDoList();
            var id1       = Guid.NewGuid();

            listModel.Id         = id1;
            listModel.CreateTime = DateTime.Now;
            listModel.Title      = "测试一下";
            listModel.UserId     = userID;
            _toDoListRepository.AddToDoList(listModel);
            await _unitOfWork.SaveAsync();

            var shareModel = new ToDoShare();

            shareModel.Id        = Guid.NewGuid();
            shareModel.ShareTime = DateTime.Now;
            shareModel.ListId    = id1;
            shareModel.UserId    = Guid.NewGuid();
            _toDoShareRepository.AddToDoShare(shareModel);
            var result = await _unitOfWork.SaveAsync();

            Assert.True(result);
        }
Пример #2
0
        public async Task <IActionResult> Post([FromBody] ToDoShareAddResource toDoShare)
        {
            if (toDoShare == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(new UnprocessableEntityObjectResult(ModelState));
            }
            var userModel = await _sysUserRepository.GetUserByAccountAync(toDoShare.Account);

            if (userModel == null)
            {
                userModel = await _sysUserRepository.GetUserByEmailAync(toDoShare.Account);
            }
            if (userModel == null)
            {
                return(NotFound("用户不存在或已删除"));
            }
            var listModel = await _toDoListRepository.GetListById(toDoShare.ListId);

            if (listModel == null)
            {
                return(NotFound("内容不存在或已删除"));
            }
            if (userModel.Id.Equals(listModel.UserId))
            {
                return(BadRequest("不能分享给自己"));
            }
            var toDoModel = new  ToDoShare();

            toDoModel.Id        = Guid.NewGuid();
            toDoModel.ListId    = listModel.Id;
            toDoModel.UserId    = userModel.Id;
            toDoModel.ShareTime = DateTime.Now;
            _toDoShareRepository.AddToDoShare(toDoModel);
            if (!await _unitOfWork.SaveAsync())
            {
                throw new Exception("Error occurred when adding");
            }
            _mailService.send(new MailOptions()
            {
                isbodyHtml = false, mailBody = $"有用户分享了待办列表《{listModel.Title}》给你", mailTitle = "待办列表分享", recipientArry = new string[1] {
                    userModel.Email
                }
            });
            return(Ok(toDoShare));
        }