Пример #1
0
        public IActionResult Post([FromBody] NewsReleaseViewModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newRelease = _mapper.Map <NewsReleaseViewModel, NewsRelease>(model);

                    _repository.AddEntity(newRelease);
                    // can assume that this always works against an in memory dataset
                    return(StatusCode(201));

                    // can be un-commented when working with a db
                    // return CreatedAtRoute("GetRelease", new { id = newRelease.Id }, newRelease);
                    //if (_repository.SaveAll())
                    //{
                    //    return CreatedAtRoute("GetRelease", new { id = newRelease.Id }, newRelease);
                    //}
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save a new release: {ex}");
            }

            return(BadRequest("Failed to save new release"));
        }
Пример #2
0
        public void Post_ShouldCreateNewEntityAndReturnSuccess()
        {
            //-----------------------------------------------------------------------------------------------------------
            // Arrange
            //-----------------------------------------------------------------------------------------------------------
            var releaseToCreate = new NewsReleaseViewModel
            {
                Key             = TestData.TestNewsRelease.Key,
                PublishDateTime = DateTime.Now
            };
            var mockRepository = CreateDataStore();

            mockRepository.Setup(r => r.AddEntity(It.IsAny <NewsRelease>())).Verifiable();
            var controller = new NewsReleasesController(mockRepository.Object, _logger.Object, _mapper.Object);

            //-----------------------------------------------------------------------------------------------------------
            // Act
            //-----------------------------------------------------------------------------------------------------------
            var result = controller.Post(releaseToCreate) as StatusCodeResult;

            //-----------------------------------------------------------------------------------------------------------
            // Assert
            //-----------------------------------------------------------------------------------------------------------
            result.Should().BeOfType(typeof(StatusCodeResult), "because the create operation should go smoothly");
            result.StatusCode.Should().Be(201, "because HTTP Status 201 should be returned upon creation of new entity");
            // this will throw if the System-Under-Test (SUT) i.e. the controller didn't call repository.AddEntity(...)
            mockRepository.Verify();
        }
Пример #3
0
        public ActionResult releaseNews(NewsReleaseViewModel model)  //发布通知公告
        {
            notices newnotices = new notices();

            newnotices.userID  = Convert.ToInt32(Request.Cookies["userID"].Value);
            newnotices.vital   = model.vital;
            newnotices.title   = model.title;
            newnotices.time    = DateTime.Now;
            newnotices.content = model.content;

            //Word.Document doc = null; //一会要记录word打开的文档
            if (Request.Files["upload-doc"] != null)
            {
                Stream fileStream = Request.Files[0].InputStream;


                string fileName   = DateTime.Now.ToString().Replace("/", ".").Replace(":", ".") + Path.GetFileName(Request.Files["upload-doc"].FileName);
                int    fileLength = Request.Files["upload-doc"].ContentLength;
                //本地存放路径
                string path = AppDomain.CurrentDomain.BaseDirectory + "schoolUploads/";
                //将文件以文件名filename存放在path路径中
                Request.Files["upload-doc"].SaveAs(Path.Combine(path, fileName));
                newnotices.attachmentAddr = path + fileName;
                newnotices.attachmentName = Request.Files["upload-doc"].FileName;
            }
            else
            {
                newnotices.attachmentAddr = "";
                newnotices.attachmentName = "";
            }
            db.notice_table.Add(newnotices);
            db.SaveChanges();
            return(RedirectToAction("Index", "Home"));
        }
        public IActionResult Put(string id, [FromBody] NewsReleaseViewModel model)
        {
            try
            {
                if (_repository.GetReleaseByKey(id) == null)
                {
                    return(NotFound($"Could not find a release with an id of {id}"));
                }
                var release = _mapper.Map <NewsReleaseViewModel, NewsRelease>(model);
                _repository.Update(id, release);
                return(Ok(model));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
            }

            return(BadRequest("Couldn't update release"));
        }