public async Task<int?> Create(AppModel model) { if (!model.IsValidForNew()) { return null; } var app = new App { UserEmail = model.Email, Name = model.Name, Url = model.Url, IsActive = true, CreatedBy = model.CreatedBy, Description = model.Description ?? string.Empty, Tokens = new List<Token> { new Token { CreatedBy = model.CreatedBy, Key = Guid.NewGuid().ToString().Replace("-", string.Empty), UsageCount = 0,IsAppActive = true } } }; _appRepository.Create(app); if (!_appRepository.SaveChanges()) return null; return await Task.FromResult(app.Id); }
public static AppModel MapFromEntity(App entity) { var model = new AppModel { Id = entity.Id, Email = entity.UserEmail, IsActive = entity.IsActive, Name = entity.Name, Description = entity.Description, Url = entity.Url != null && entity.Url.StartsWith("http") ? entity.Url : string.Format("http://{0}", entity.Url) }; var tokens = entity.Tokens.Where(x => !x.IsDeleted); foreach (var token in tokens) { model.Tokens.Add(new TokenModel { CreationDate = token.CreatedAt, UsageCount = token.UsageCount, Token = token.Key }); } return model; }
public ActionResult New(AppModel model) { if (model.IsValidForNew()) { return Redirect("/user/apps"); } model.Msg = "bir sorun oluştu..."; return View(model); }
public async void create_should_return_null_if_model_is_invalid() { //arrange var invalidModel = new AppModel { Name = "invalidApp" }; //act var sut = new AppServiceBuilder().Build(); var task = await sut.Create(invalidModel); //assert Assert.Null(task); }
public void new_should_redirect_if_model_is_valid() { // Arrange var validModel = new AppModel { Name = "test name", Url = "test.com", Description = "test description" }; // Act var controller = new AppController(null, null); var view = controller.New(validModel) as RedirectResult; // Assert Assert.NotNull(view); Assert.AreEqual(view.Url, "/user/apps"); Assert.IsTrue(controller.HasPostAttribute("New", new[] { typeof(AppModel) }), "HttpPost attribute not found or ValidateAntiForgeryToken attribute not added on AppController's New() action method"); }
public void new_should_return_app_model_if_model_is_invalid() { // Arrange var inValidModel = new AppModel { Name = "test name", Url = "test.com" }; // Act var controller = new AppController(null, null); var view = controller.New(inValidModel) as ViewResult; // Assert Assert.NotNull(view); Assert.NotNull(view.Model); var model = view.Model as AppModel; Assert.NotNull(model); Assert.IsTrue(controller.HasPostAttribute("New", new[] { typeof(AppModel) }), "HttpPost attribute not found or ValidateAntiForgeryToken attribute not added on AppController's New() action method"); }
public void new_should_return_app_model_if_model_is_invalid() { // Arrange var appService = new Mock<IAppService>(); var inValidModel = new AppModel { Name = "test name", Url = "test.com" }; // Act var controller = new AppController(null, null, appService.Object); var view = controller.New(inValidModel).Result as ViewResult; // Assert Assert.NotNull(view); Assert.NotNull(view.Model); var model = view.Model as AppModel; Assert.NotNull(model); controller.AssertPostAttribute("New", new[] { typeof(AppModel) }); }
public async Task<ActionResult> New(AppModel model) { if (!model.IsValidForNew()) { model.Msg = "bir sorun oluştu..."; return View(model); } model.CreatedBy = User.Identity.GetUserId(); model.Email = User.Identity.GetUserEmail(); var appId = await _appService.Create(model); if (appId == null) { model.Msg = "bir sorun oluştu..."; return View(model); } return Redirect("/app/detail/" + appId); }
public async void create_should_return_with_app_id_if_model_is_valid() { //arrange var appModel = new AppModel { Name = "test", Url = "test.com", Description = "test_desc" }; var appRepository = new Mock<IRepository<App>>(); appRepository.Setup(x => x.Create(It.IsAny<App>())).Returns(new App()); appRepository.Setup(x => x.SaveChanges()).Returns(true); //act var sut = new AppServiceBuilder().WithAppRepository(appRepository.Object) .Build(); var appId = await sut.Create(appModel); //assert Assert.NotNull(appId); Assert.IsAssignableFrom<int>(appId); appRepository.Verify(x => x.Create(It.IsAny<App>()), Times.Once); appRepository.Verify(x => x.SaveChanges(), Times.AtLeastOnce); }
public void new_should_return_app_model_if_model_is_invalid() { //arrange var appService = new Mock<IAppService>(); var inValidModel = new AppModel { Name = "test name", Url = "test.com" }; //act var sut = new AppControllerBuilder().WithAppService(appService.Object) .Build(); var view = sut.New(inValidModel).Result as ViewResult; //assert Assert.NotNull(view); Assert.NotNull(view.Model); var model = view.Model as AppModel; Assert.NotNull(model); sut.AssertPostAndAntiForgeryTokenAttribute(ActionNameNew, new[] { typeof(AppModel) }); }
public static AppModel MapFromEntity(App entity) { var model = new AppModel(); model.Id = entity.Id; model.Email = entity.UserEmail; model.IsActive = entity.IsActive; model.Name = entity.Name; model.Description = entity.Description; model.Url = entity.Url; var tokens = entity.Tokens.Where(x => !x.IsDeleted); foreach (var token in tokens) { model.Tokens.Add(new TokenModel { CreationDate = token.CreatedAt, UsageCount = token.UsageCount, Token = token.Key }); } return model; }
public ActionResult New() { var model = new AppModel(); return View(model); }