public async Task <Category> CreateCategoryAsync(AddCategoryRequestModel requestModel) { if (requestModel.Name != null) { requestModel.Name = requestModel.Name; } if (requestModel.Description != null) { requestModel.Description = requestModel.Description; } Category aCategory = new Category() { Name = requestModel.Name, Description = requestModel.Description, }; var applicationUserId = requestModel.Myuser.Claims.Where(c => c.Type == "sub") .Select(c => c.Value).SingleOrDefault(); aCategory.ApplicationUserId = Convert.ToInt64(applicationUserId); await _context.AddAsync(aCategory); if (await _context.SaveChangesAsync() > 0) { return(aCategory); } return(null); }
public async Task Add_Category_Name_Exists() { var client = _factory.CreateClient(); var name = TestHelpers.GetRandomString(); AddCategoryRequestModel objCat = new AddCategoryRequestModel { Name = name }; //Adding Random Category await client.PostAsync("api/Category/Add", new StringContent( JsonConvert.SerializeObject(objCat), Encoding.UTF8, "application/json")); // getting test result with already created category var response = await client.PostAsync("api/Category/Add", new StringContent( JsonConvert.SerializeObject(objCat), Encoding.UTF8, "application/json")); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); }
public async Task <ApiResponse> Add(AddCategoryRequestModel options) { ApiResponse response = new ApiResponse(); var category = await _context.Categories.Where(x => x.Name == options.Name).FirstOrDefaultAsync(); if (category != null) { response.Message = $"{options.Name} already exists."; return(response); } int catCount = await _context.Categories.CountAsync(); Category objCat = new Category { IsActive = true, CatCode = $"{ options.Name.Substring(0, 3).ToUpper()}-{catCount + 1}", IsRemoved = false, LastUpdated = DateTime.Now, Name = options.Name }; await _context.Categories.AddAsync(objCat); await _context.SaveChangesAsync(); response.Message = $"{options.Name} saved successfully"; response.Status = 1; return(response); }
public async Task <IActionResult> Update(int catId, [FromBody] AddCategoryRequestModel options) { var cat = await categoryService.Update(catId, options); if (cat.Status == 1) { return(Ok(cat)); } else { return(NotFound(cat)); } }
public async Task <IActionResult> Add([FromBody] AddCategoryRequestModel options) { var cat = await categoryService.Add(options); if (cat.Status == 1) { return(Ok(cat)); } else { return(BadRequest(cat)); } }
public async Task <IActionResult> CreateCategory([FromForm] AddCategoryRequestModel request) { request.Myuser = User; var category = await _categoryRepository.CreateCategoryAsync(request); if (category != null) { return(Ok(category)); } return(BadRequest()); }
public async Task Category_Model_State_Test() { var client = _factory.CreateClient(); AddCategoryRequestModel objCat = new AddCategoryRequestModel { Name = "" }; var response = await client.PostAsync("api/Category/Add", new StringContent( JsonConvert.SerializeObject(objCat), Encoding.UTF8, "application/json")); Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); }
public async Task Add_Category_Test() { var client = _factory.CreateClient(); var name = TestHelpers.GetRandomString(); AddCategoryRequestModel objCat = new AddCategoryRequestModel { Name = name }; var response = await client.PostAsync("api/Category/Add", new StringContent( JsonConvert.SerializeObject(objCat), Encoding.UTF8, "application/json")); Assert.Equal(HttpStatusCode.OK, response.StatusCode); }
public async Task Update_Category_Not_Found_Test() { var client = _factory.CreateClient(); var name = TestHelpers.GetRandomString(); AddCategoryRequestModel objCat = new AddCategoryRequestModel { Name = name }; //Here we need to ensure that category is not available in database var response = await client.PutAsync("api/Category/Update/999999", new StringContent( JsonConvert.SerializeObject(objCat), Encoding.UTF8, "application/json")); Assert.Equal(HttpStatusCode.NotFound, response.StatusCode); }
public async Task <ApiResponse> Update(int catId, AddCategoryRequestModel options) { ApiResponse response = new ApiResponse(); var category = await _context.Categories.FirstOrDefaultAsync(x => x.CategoryId == catId); if (category == null) { response.Message = $"No category found with id : {catId}"; return(response); } category.LastUpdated = DateTime.Now; category.Name = options.Name; await _context.SaveChangesAsync(); response.Message = $"{options.Name} updated successfully"; response.Status = 1; return(response); }