private MenuState TryToCreatePost() { var isCategoryExisting = this.categoryService.GetByName(this.Post.Category) != null; if (!isCategoryExisting) { this.categoryService.Create(this.Post.Category); } var serviceModel = new PostServiceModel() { Title = this.Post.Title, Content = string.Join(string.Empty, this.Post.Content), Category = this.Post.Category, Author = this.Post.Author }; var postAddedSuccessfull = this.postService.Create(serviceModel); if (!postAddedSuccessfull) { this.Error = true; return(MenuState.Rerender); } return(MenuState.PostAdded); }
public PostViewModel(PostServiceModel model) { this.PostId = model.PostId; this.Title = model.Title; this.Author = model.Author; this.Category = model.Category; this.Content = GetLines(model.Content); this.Replies = this.ConvertReplyServiceModels(model.Replies); }
public string Post([FromBody] PostServiceModel value) { try{ var service = value.ToServiceModel(); var validation = ValidateService(service); if (validation.Length == 0) { _repo.Create(service); return(JsonConvert.SerializeObject(new ResultModel("Service Created", false))); } return(JsonConvert.SerializeObject(new ResultModel(validation.Aggregate((a, b) => $"{a} | {b}"), true))); } catch (Exception ex) { return(JsonConvert.SerializeObject(new ResultModel(ex.Message, true))); } }
public bool Create(PostServiceModel model) { using (var db = new ForumSystemDbContext()) { var newPost = new Post() { Title = model.Title, Content = model.Content, AuthorId = db.Users.FirstOrDefault(u => u.Username == model.Author).Id, CategoryId = db.Categories.First(c => c.Name == model.Category).Id }; if (newPost.IsValid()) { db.Posts.Add(newPost); db.SaveChanges(); return(true); } return(false); } }
public void Should_not_create_or_update_service_on_post_put_when_provided_invalid_service_vehicle() { //Arrange var _serviceRepo = new Mock <IRepository <ServiceModel> >(); var _vehicleRepo = new Mock <IRepository <VehicleModel> >(); var id = Guid.NewGuid(); _vehicleRepo.Setup(setup => setup.Read()).Returns(new System.Collections.Generic.List <VehicleModel>() { new VehicleModel() { Id = id, VehicleType = VehicleTypeEnum.Diesel } }); var post = new PostServiceModel() { VehicleId = id, ProviderId = Guid.NewGuid(), ServiceType = MaintenanceTypeEnum.BatteryChange, Cost = 12, Date = DateTime.Now, Note = "", Odometer = 0 }; _controller = new ServicesController(_serviceRepo.Object, new ServicesValidation(), _vehicleRepo.Object); //Act var result = JsonConvert.DeserializeObject <ResultModel>(_controller.Post(post)); var result1 = JsonConvert.DeserializeObject <ResultModel>(_controller.Put(post.ToServiceModel())); //Assert Assert.True(result.IsError); Assert.True(result1.IsError); }