public async Task <ApiResponse> CreateOrEditCategory(CategoryDto dto) { if (!ModelState.IsValid) { throw new ApiException(ModelState.AllErrors()); } // Case insert if (dto.Id == Guid.Empty) { var category = _mapper.Map <Category>(dto); category.UpdatedDate = null; await _categoryService.AddAsync(category); return(new ApiResponse("New record has been created to the database", dto, 201)); } var categoryOld = await _categoryService.GetByIdAsync(dto.Id); if (categoryOld != null) { var newCategory = _mapper.Map(dto, categoryOld); newCategory.UpdatedDate = DateTime.Now; await _categoryService.UpdateAsync(newCategory); return(new ApiResponse($"Record has been updated with id {dto.Id} to the database", dto, 201)); } throw new ApiException($"Record with id: {dto.Id} does not exist.", 400); }
public void AddAsync_CategoryAlreadyExists_ThrowsDuplicationException() { // Arrange var category = new Category { Id = 1, Name = "Test" }; var categoryToMatch = new Category { Id = 2, Name = "Test" }; _postUnitOfWorkMock.Setup(x => x.CategoryRepository) .Returns(_categoryRepositoryMock.Object); _categoryRepositoryMock.Setup(x => x.IsExistsAsync( It.Is <Expression <Func <Category, bool> > >(y => y.Compile()(categoryToMatch)))) .ReturnsAsync(true).Verifiable(); // Act Should.Throw <DuplicationException>(() => _categoryService.AddAsync(category)); // Assert _categoryRepositoryMock.VerifyAll(); }
public async Task <ApiResponse> createCategory([FromBody] CategoryDto dto) { if (dto.Id == Guid.Empty) { var categoryDto = _mapper.Map <Category>(dto); await _categoryService.AddAsync(categoryDto); var vm = _mapper.Map <CategoryViewModel>(categoryDto); return(new ApiResponse("success", vm, 200)); } return(new ApiResponse("No item", null, 200)); }
public async Task <IActionResult> Add(CategoryAddDto categoryAddDto) { if (ModelState.IsValid) { var result = await _categoryService.AddAsync(categoryAddDto, LoggedInUser.UserName); if (result.ResultStatus == ResultStatus.Success) { var categoryAddAjaxModel = JsonSerializer.Serialize(new CategoryAddAjaxViewModel { CategoryDto = result.Data, CategoryAddPartial = await this.RenderViewToStringAsync("_CategoryAddPartial", categoryAddDto) }); return(Json(categoryAddAjaxModel)); } } var categoryAddAjaxErrorModel = JsonSerializer.Serialize(new CategoryAddAjaxViewModel { CategoryAddPartial = await this.RenderViewToStringAsync("_CategoryAddPartial", categoryAddDto) }); return(Json(categoryAddAjaxErrorModel)); }
public async Task <IActionResult> Save(CategoryDto categoryDto) { var categoryToSave = await _categoryService.AddAsync(_mapper.Map <Category>(categoryDto)); // null => URL of the newly added category return(Created(string.Empty, _mapper.Map <CategoryDto>(categoryToSave))); }
public async Task <IActionResult> Create(EventViewModel model, int competitionInstanceId, int competitionId) { ViewBag.Disciplines = _disciplineService.GetAllDisciplines(); ViewBag.InstanceId = model.CompetitionInstanceId; if (ModelState.IsValid && model.Name != null) { try { var _event = await _eventService.AddAsync(model); // Create a Overall Category automatically var overall = new CategoryViewModel { Name = "Overall", AgeFrom = 0, AgeTo = 99, CountryId = 0, EventId = _event.Id, Gender = Gender.All }; await _categoryService.AddAsync(overall); } catch (Exception e) { return(Json(e.Message)); } return(RedirectToAction("CompetitionInstance", "Admin", new { competitionId, competitionInstanceId })); } return(View(model)); }
public async Task <IActionResult> CreateCategoryAsync([FromBody] CategoryResource categoryResource) { if (categoryResource == null) { ProblemDetails problem = new ProblemDetails { Title = "Failed to create a new category.", Detail = "The specified category resource was null", Instance = "ABA3B997-1B80-47FC-A72B-69BC0D8DFA93" }; return(BadRequest(problem)); } Category category = mapper.Map <CategoryResource, Category>(categoryResource); try { await categoryService.AddAsync(category) .ConfigureAwait(false); categoryService.Save(); return(Created(nameof(CreateCategoryAsync), mapper.Map <Category, CategoryResourceResult>(category))); } catch (DbUpdateException e) { Log.Logger.Error(e, "Database exception"); ProblemDetails problem = new ProblemDetails { Title = "Failed to save the new category.", Detail = "There was a problem while saving the category to the database.", Instance = "D56DBE55-57A1-4655-99C5-4F4ECEEE3BE4" }; return(BadRequest(problem)); } }
private async Task <string> CreateRoundAsync() { var(_, games) = await m_gameService.SearchAsync(new Pagination(), new SimpleFilter <Game>()); var game = games.First(); var round = new Round { Id = Guid.NewGuid(), GameId = game.Id }; await m_roundService.AddAsync(round); var rand = new Random(); var categoryValue = listOfCategories.ElementAt(rand.Next(0, listOfCategories.Count)).Key; var category = new Category { Id = Guid.NewGuid(), RoundId = round.Id, Value = categoryValue }; await m_categoryService.AddAsync(category); foreach (KeyValuePair <string, List <string> > entry in listOfCategories) { if (entry.Key.Equals(categoryValue)) { foreach (string value in entry.Value) { await m_correctResponsesService.AddAsync(new CorrectResponses { Id = Guid.NewGuid(), CategoryId = category.Id, Values = value }); } } } return(categoryValue); }
private async void btnSave_Click(object sender, EventArgs e) { try { if (txtName.Text == "") { MetroMessageBox.Show(this, "Name is Required", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); txtName.WithError = true; return; } var category = new CategoryModel(); category.Name = txtName.Text; //edit if (id > 0) { await _categoryService.EditAsync(id, category); } else { //add await _categoryService.AddAsync(category); } await LoadCategories(); } catch (CustomBaseException ex) { MetroMessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception ex) { MetroMessageBox.Show(this, ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public async Task <ActionResult> Detail(Guid id, CategoryDetailVM vm) { if (!ModelState.IsValid) { return(View(vm)); } APIResultVM result = new APIResultVM(); var user = await _userManager.FindByNameAsync(User.Identity.Name); if (id.IsNull() || id == Guid.Empty) { result = await _service.AddAsync(vm.Rec, user.Id); } else { result = await _service.UpdateAsync(id, vm.Rec, user.Id); } if (!result.IsSuccessful) { if (result.Messages.Any()) { foreach (var error in result.Messages) { ModelState.AddModelError("GeneralError", error); } } return(View(vm)); } return(RedirectToAction("Index", "Category")); }
public async Task <IActionResult> Save(CategoryDto categoryDto) { var newCategory = await _categoryService.AddAsync(_mapper.Map <Category>(categoryDto)); return(Created(string.Empty, _mapper.Map <CategoryDto>(newCategory))); // return Created(string.Empty, _mapper.Map<CategoryDto>(newCategory)); }
public async Task <IActionResult> Save(CategoryDto categoryDto) { var newCategory = await _categoryService.AddAsync(_mapper.Map <Category>(categoryDto)); //201 kodu dondurebılmek ıcın Created kullanıyoruz BPractice acısından return(Created(string.Empty, _mapper.Map <CategoryDto>(newCategory))); }
public async Task <IActionResult> Post([FromBody] Category model) { await _categoryService.AddAsync(model); return(NoContent()); //return Json($"Called Category POST. Id: {model.CategoryId} - name: '{model.Name}'"); }
public async Task <IActionResult> CreateNew([FromBody] ApiV1CategoryCreateRequestModel model) { logger.LogDebug($"Attempting to create new category."); if (model != null && ModelState.IsValid) { // Check if a category with the requested name exists already if (categoryService.DoesCategoryNameExist(model.Title)) { logger.LogWarning($"A category with the name {model.Title} exists already. Aborting request."); return(BadRequest(new ApiV1ErrorResponseModel("A category with this name exists already."))); } var categoryEntity = new CategoryEntity { Title = model.Title }; await categoryService.AddAsync(categoryEntity); logger.LogInformation($"Successfully added new category with title {model.Title}."); return(Json(new { categoryEntity.Id, categoryEntity.Title })); } else { logger.LogWarning($"Error while creating new category. Validation failed."); return(BadRequest(ModelState.ToApiV1ErrorResponseModel())); } }
public async Task <IActionResult> Create(CategoryDTO categoryDTO) { var category = _mapper.Map <Category>(categoryDTO); await _categoryService.AddAsync(category); return(RedirectToAction("Index")); }
public async Task AddAsync_Null_ThrowRutrackerException() { // Act & Assert var exception = await Assert.ThrowsAsync <RutrackerException>(async() => await _categoryService.AddAsync(null)); Assert.Equal(ExceptionEventTypes.InvalidParameters, exception.ExceptionEventType); }
public async Task <IActionResult> CreateCategoryAsync([FromBody] CreateCategory command) { var category = await _categoryService.AddAsync(command); var categoryDto = _mapper.Map <CategoryDto>(category); return(Created($"{Request.Host}{Request.Path}/{category.Id}", categoryDto)); }
public async Task <IActionResult> Add([FromBody] Category categoryModel) { return(await OnActionWorkAsync(async() => { await _categoryService.AddAsync(categoryModel); return Ok("ok"); })); }
public async Task <IActionResult> Post([FromBody] Domain.Models.Category category) { await _categoryService.AddAsync(category); return(Created($"/api/category/{category.Id}", new Domain.Models.Category { Id = category.Id })); }
public async Task <IActionResult> AddAsync([FromBody] Category category) { var user = await _userManager.GetUserAsync(User); category.CreatedBy = user.Id; category.ModifiedBy = user.Id; return(CreatedAtAction(nameof(AddAsync), await _categoryService.AddAsync(category))); }
public async Task <CategoryView> Post(CategoryCreateView model) { var category = _mapper.Map <Category>(model); var result = await _categoryService.AddAsync(category); return(_mapper.Map <CategoryView>(result)); }
public async Task <IActionResult> Post(Category category) { var result = await categoryService.AddAsync(category); var json = JsonSerializer.Serialize(result); await cache.SetAsync("cs", Encoding.UTF8.GetBytes(json)); return(CreatedAtAction(nameof(Get), new { id = result.CategoryId }, result)); }
public async Task <IActionResult> Create(CategoryDto categoryDto) { if (ModelState.IsValid) { await _categoryService.AddAsync(categoryDto); return(RedirectToAction(nameof(Index))); } return(View(categoryDto)); }
public async Task <IActionResult> AddCategory(CategoryAddDto model) { if (ModelState.IsValid) { await _categoryService.AddAsync(_mapper.Map <Category>(model)); return(RedirectToAction("Index", "Category", new { area = "Member" })); } return(View(model)); }
public async Task <IActionResult> AddAsync(Category category) { var result = await _categoryService.AddAsync(category); if (result.Success) { return(Ok(JsonConvert.SerializeObject(result.Message))); } return(BadRequest(JsonConvert.SerializeObject(result.Message))); }
public async Task <IActionResult> Create(CategoryViewModel model, int competitionId, int competitionInstanceId, int eventId) { if (ModelState.IsValid && model.Name != null) { await _categoryService.AddAsync(model); return(RedirectToAction("Categories", "Admin", new { competitionId, competitionInstanceId, eventId })); } return(View(model)); }
public async Task <IActionResult> Add(CategoryAddDto category) { if (ModelState.IsValid) { await _categoryService.AddAsync(category.Adapt <Category>()); return(RedirectToAction("Index", "Category", new { area = "Admin" })); } return(View(category)); }
public async Task <IActionResult> Create([FromBody] CategoryVM category) { if (category == null) { return(BadRequest("The category model is null")); } await _categoryService.AddAsync(_mapper.Map <CategoryDTO>(category)); return(Ok()); }
public async Task <IActionResult> CreateCategory([Bind("Name,Description,Id")] CategoryDto category) { if (ModelState.IsValid) { await categoryService.AddAsync(category); return(RedirectToAction("CategoryManage", "Manage")); } return(View("Category/Create", category)); }
public async Task <IActionResult> AddCategory([FromBody] CategoryDTO category) { if (!ModelState.IsValid) { return(BadRequest() as IActionResult); } var info = await service.AddAsync(category); return(info ? Ok() : StatusCode(400)); }