public async Task<IResult<Collection>> GetCollectionAsync(string resourceId) { IResult<Collection> result = null; if (resourceId.IsNullOrEmpty()) { return new ShopTimeResult<Collection>(){Error = new Error(){CodeNumber = 500, Message = "resourceId can't be null or empty string"}}; } try { var map = new UrlToTags(resourceId); if (map.IsValid == false) { result = new ShopTimeResult<Collection>(); return result; } var collection = _collectionRepo.Table.SingleOrDefault(obj => obj.ResourceId.ToLower() == map.ResourceId && obj.StoreId == _token.Store.Id && obj.IsDeleted == false); if (collection != null) { if (collection.Tags.IsNullOrEmpty() && map.Tags.IsNullOrEmpty() == false) { collection = null; } else if (collection.Tags.IsNullOrEmpty() == false && map.Tags.IsNullOrEmpty() == false) { foreach (var tag in map.Tags) { if (collection.Tags.Contains(tag) == false) { collection = null; break; } } } } result = new ShopTimeResult<Collection>(collection); } catch (Exception ex) { result = new ShopTimeResult<Collection> { Error = new Error() { CodeNumber = 500, Message = ex.Message } }; } return result; }
public async Task<IResult<Collection>> GetCollectionAsync(int collectionId) { IResult<Collection> result = null; try { var collection = _collectionRepo.GetById(collectionId); if (collection == null || collection.StoreId != _token.Store.Id) { result = new ShopTimeResult<Collection>(); } result = new ShopTimeResult<Collection>(collection); } catch (Exception ex) { result = new ShopTimeResult<Collection> {Error = new Error() {CodeNumber = 500, Message = ex.Message}}; } return result; }
public async Task<IResult<IList<Collection>>> GetCollectionsAsync(int? limit = null, int? page = null, CatalogSortBy? sortBy = null, bool isTracking = true) { IResult<IList<Collection>> result = null; var pager = new Pager(limit, page); try { var collectionQry = _collectionRepo.Table.Where(obj => obj.StoreId == _token.Store.Id && obj.IsDeleted == false); switch(sortBy) { case CatalogSortBy.DiplayNameAsc: collectionQry = collectionQry.OrderBy(obj => obj.DisplayName); break; default: collectionQry = collectionQry.OrderBy(obj => obj.Id); break; } collectionQry = collectionQry.Skip(pager.Skip).Take(pager.Take); IList<Collection> collections = null; if (isTracking) { collections = collectionQry.ToList(); } else { collections = collectionQry.AsNoTracking().ToList(); } result = new ShopTimeResult<IList<Collection>>(collections); } catch (Exception ex) { result = new ShopTimeResult<IList<Collection>> { Error = new Error() { CodeNumber = 500, Message = ex.Message } }; } return result; }
public async Task<IResult> DeleteCollectionAsync(Collection collection) { IResult result = null; if (collection == null) { result = new ShopTimeResult() { Error = new Error() { CodeNumber = 500, Message = "collection can't be null." } }; return result; } //get all custom fields and set to be deleted. var getCustomFieldsTask = GetCustomFieldsAsync(collection.Id, true); collection.IsDeleted = true; var updateCollectionTask = _collectionRepo.UpdateAsync(collection); Task.WaitAll(getCustomFieldsTask); if (getCustomFieldsTask.Result.Error != null) { result = new ShopTimeResult() { Error = getCustomFieldsTask.Result.Error }; return result; } Task<IResult> deleteCustomFieldsTask = null; if (getCustomFieldsTask.Result.ResultValue.IsNullOrEmpty() == false) { deleteCustomFieldsTask = DeleteCustomFieldsAsync(getCustomFieldsTask.Result.ResultValue); Task.WaitAll(updateCollectionTask, deleteCustomFieldsTask); if (deleteCustomFieldsTask.Result.Error != null) { result = new ShopTimeResult() { Error = deleteCustomFieldsTask.Result.Error }; return result; } } else { Task.WaitAll(updateCollectionTask); } if (updateCollectionTask.Result.Error != null) { result = new ShopTimeResult() { Error = updateCollectionTask.Result.Error }; return result; } result = new ShopTimeResult(); return result; }
public void GetCountTest() { var countResult = new ShopTimeResult<int>(8); var notFoundResult = new ShopTimeResult<int> {Error = new Error() {CodeNumber = 404}}; var countTask = Task.FromResult<IResult<int>>(countResult); var notFoundTask = Task.FromResult<IResult<int>>(notFoundResult); _collectionServiceMock.Setup(x => x.GetCollectionsCountAsync()).Returns(countTask); _collectionServiceMock.Setup(x => x.GetCollectionsCountAsync(3)).Returns(countTask); _collectionServiceMock.Setup(x => x.GetCollectionsCountAsync(99)).Returns(notFoundTask); var collectionsController = new CollectionsV1Controller(_collectionServiceMock.Object); //return count test var actionResult = collectionsController.GetCollectionCountAsync().Result; var response = actionResult as OkNegotiatedContentResult<int>; response.Content.Should().Be(8); actionResult = collectionsController.GetCollectionCountAsync(3).Result; response = actionResult as OkNegotiatedContentResult<int>; response.Content.Should().Be(8); //return not found actionResult = collectionsController.GetCollectionCountAsync(99).Result; actionResult.Should().BeOfType<NotFoundResult>(); _collectionServiceMock.VerifyAll(); }
public async Task<IResult> DeleteCustomFieldsAsync(IList<CustomField> customFields) { IResult result = null; if (customFields.IsNullOrEmpty()) { result = new ShopTimeResult() { Error = new Error() { Message = "customFields can't be null or empty."} }; } try { foreach (var customField in customFields) { customField.IsDeleted = true; } await _customFieldRepo.UpdateAsync(customFields) .Error(err => { result = new ShopTimeResult() { Error = err}; }) .Success(() => { result = new ShopTimeResult(); }); } catch (Exception ex) { result = new ShopTimeResult() { Error = new Error() { CodeNumber = 500, Message = ex.Message } }; } return result; }
public async Task<IResult<CustomField>> GetCustomFieldAsync(int collectionId, string key) { IResult<CustomField> result = null; try { var customField = _customFieldRepo.Table.SingleOrDefault(obj => obj.StoreId == _token.Store.Id && obj.Type == CustomFieldType.Collection && obj.ParentId == collectionId && obj.Key.ToLower() == key.Trim().ToLower() && obj.IsDeleted == false); result = new ShopTimeResult<CustomField>(customField); } catch (Exception ex) { result = new ShopTimeResult<CustomField> { Error = new Error() { CodeNumber = 500, Message = ex.Message } }; } return result; }
public async Task GetCollectionTest() { var menCollection = new Collection { Id = 7, ResourceId = "men", DisplayName = "DisplayName_men", Description = "Desc", Tags = new List<string>() {"jean, polo"}, IsVisible = true, //Image = new Image() {Url = "http://www.example.com/public/collection/1/DisplayName_men.jpg"} }; var collectionResult = new ShopTimeResult<Collection>(menCollection); var notFoundResult = new ShopTimeResult<Collection> {Error = new Error() {CodeNumber = 404}}; var badResult = new ShopTimeResult<Collection> {Error = new Error()}; var collectionTask = Task.FromResult<IResult<Collection>>(collectionResult); var notFoundTask = Task.FromResult<IResult<Collection>>(notFoundResult); var badTask = Task.FromResult<IResult<Collection>>(badResult); //arrange _collectionServiceMock.Setup(x => x.GetCollectionAsync(99)).Returns(notFoundTask); _collectionServiceMock.Setup(x => x.GetCollectionAsync(7)).Returns(collectionTask); _collectionServiceMock.Setup(x => x.GetCollectionAsync(0)).Returns(badTask); var collectionsController = new CollectionsV1Controller(_collectionServiceMock.Object); //return notFound var task1 = collectionsController.GetCollectionAsync(99).ContinueWith(task => { task.Result.Should().BeOfType<NotFoundResult>(); Console.WriteLine("task1:{0}", Thread.CurrentThread.ManagedThreadId); }); Console.WriteLine("main:{0}", Thread.CurrentThread.ManagedThreadId); //return single collectionModel var task2 = collectionsController.GetCollectionAsync(7).ContinueWith(task => { var response = task.Result as OkNegotiatedContentResult<CollectionModel>; var model = response.Content; model.Id.Should().Be(menCollection.Id); model.ResourceId.Should().Be(menCollection.ResourceId); model.DisplayName.Should().Be(menCollection.DisplayName); model.Description.Should().Be(menCollection.Description); model.IsVisible.Should().BeTrue(); //model.Image.Url.Should().Be(menCollection.Image.Url); model.Products.Should().BeNull(); model.CustomFields.Should().BeNull(); Console.WriteLine("task2:{0}", Thread.CurrentThread.ManagedThreadId); }); //return bad reqesut var task3 = collectionsController.GetCollectionAsync(0).ContinueWith(task => { task.Result.Should().BeOfType<BadRequestResult>(); Console.WriteLine("task3:{0}", Thread.CurrentThread.ManagedThreadId); }); Task.WaitAll(task1, task2, task3); Console.WriteLine("done:{0}", Thread.CurrentThread.ManagedThreadId); _collectionServiceMock.VerifyAll(); }
public void DeleteCustomFieldTest() { //arrange var metaResult = new ShopTimeResult(); var metaTask = Task.FromResult<IResult>(metaResult); _collectionServiceMock.Setup(x => x.DeleteCustomFieldAsync(8, 3)).Returns(metaTask); var notFoundResult = new ShopTimeResult() { Error = new Error() { CodeNumber = 404 } }; var notFoundTask = Task.FromResult<IResult>(notFoundResult); _collectionServiceMock.Setup(x => x.DeleteCustomFieldAsync(99, 5)).Returns(notFoundTask); var badResult = new ShopTimeResult<int> { Error = new Error() }; var badTask = Task.FromResult<IResult>(badResult); _collectionServiceMock.Setup(x => x.DeleteCustomFieldAsync(4, 7)).Returns(badTask); var collectionsController = new CollectionsV1Controller(_collectionServiceMock.Object); //OK result var actionResult = collectionsController.DeleteCustomFieldAsync(8, 3).Result; actionResult.Should().BeOfType<OkResult>(); //result not found test actionResult = collectionsController.DeleteCustomFieldAsync(99, 5).Result; actionResult.Should().BeOfType<NotFoundResult>(); //bad request actionResult = collectionsController.DeleteCustomFieldAsync(4, 7).Result; actionResult.Should().BeOfType<BadRequestResult>(); _collectionServiceMock.VerifyAll(); }
public void CreateCustomFieldTest() { var metaData = new CustomField(); metaData.Id = 5; metaData.Key = "App_Code"; metaData.Value = "ABC123"; var metaResult = new ShopTimeResult<int>(5) {ResultValue = metaData.Id}; var notFoundResult = new ShopTimeResult<int> {Error = new Error() {CodeNumber = 404}}; var badResult = new ShopTimeResult<int> {Error = new Error()}; var metaTask = Task.FromResult<IResult<int>>(metaResult); var noutFoundTask = Task.FromResult<IResult<int>>(notFoundResult); var badTask = Task.FromResult<IResult<int>>(badResult); _collectionServiceMock.Setup(x => x.CreateCustomFieldAsync(6, It.IsAny<CustomField>())).Returns(metaTask); _collectionServiceMock.Setup(x => x.CreateCustomFieldAsync(99, It.IsAny<CustomField>())).Returns(noutFoundTask); _collectionServiceMock.Setup(x => x.CreateCustomFieldAsync(4, It.IsAny<CustomField>())).Returns(badTask); var collectionsController = new CollectionsV1Controller(_collectionServiceMock.Object); //return result var actionResult = collectionsController.CreateCustomFieldAsync(6, new CustomFieldModel()).Result; var response = actionResult as CreatedAtRouteNegotiatedContentResult<int>; response.Content.Should().Be(metaData.Id); response.RouteName.Should().Be("GetCustomFieldById"); response.RouteValues["collectionId"].Should().Be(6); response.RouteValues["metaDataId"].Should().Be(metaData.Id); //result not found test actionResult = collectionsController.CreateCustomFieldAsync(99, new CustomFieldModel()).Result; actionResult.Should().BeOfType<NotFoundResult>(); //bad request actionResult = collectionsController.CreateCustomFieldAsync(4, new CustomFieldModel()).Result; actionResult.Should().BeOfType<BadRequestResult>(); _collectionServiceMock.VerifyAll(); }
public void UpdateCustomFieldsTest() { //arrange var getCollection8Result = new ShopTimeResult<Collection>(); var getCollection8ResultTask = Task.FromResult<IResult<Collection>>(getCollection8Result); _collectionServiceMock.Setup(x => x.GetCollectionAsync(8)).Returns(getCollection8ResultTask); var getCField8Result = new ShopTimeResult<CustomField>(); var getCField8ResultTask = Task.FromResult<IResult<CustomField>>(getCField8Result); _collectionServiceMock.Setup(x => x.GetCustomFieldAsync(8,8)).Returns(getCField8ResultTask); var metaResult = new ShopTimeResult(); var metaTask = Task.FromResult<IResult>(metaResult); _collectionServiceMock.Setup(x => x.UpdateCustomFieldAsync(It.IsAny<CustomField>())).Returns(metaTask); var notFoundResult = new ShopTimeResult() { Error = new Error() { CodeNumber = 404 } }; var notFoundTask = Task.FromResult<IResult>(notFoundResult); _collectionServiceMock.Setup(x => x.UpdateCustomFieldAsync(It.Is<CustomField>(y => y.Id == 8))).Returns(notFoundTask); var badResult = new ShopTimeResult<int> { Error = new Error() }; var badTask = Task.FromResult<IResult>(badResult); _collectionServiceMock.Setup(x => x.UpdateCustomFieldAsync(It.IsAny<CustomField>())).Returns(badTask); var collectionsController = new CollectionsV1Controller(_collectionServiceMock.Object); //OK result var actionResult = collectionsController.UpdaetCustomFieldAsync(8, 8, new CustomFieldModel()).Result; actionResult.Should().BeOfType<OkResult>(); //result not found test actionResult = collectionsController.UpdaetCustomFieldAsync(99, 8, new CustomFieldModel()).Result; actionResult.Should().BeOfType<NotFoundResult>(); //bad request actionResult = collectionsController.UpdaetCustomFieldAsync(4, 8, new CustomFieldModel()).Result; actionResult.Should().BeOfType<BadRequestResult>(); _collectionServiceMock.VerifyAll(); }
public void GetCustomFieldsCountTest() { var metaResult = new ShopTimeResult<int>(0); var notFoundResult = new ShopTimeResult<int> {Error = new Error() {CodeNumber = 404}}; var metaTask = Task.FromResult<IResult<int>>(metaResult); var notFoundTask = Task.FromResult<IResult<int>>(notFoundResult); _collectionServiceMock.Setup(x => x.GetCustomFieldsCountAsync(118)).Returns(metaTask); _collectionServiceMock.Setup(x => x.GetCustomFieldsCountAsync(99)).Returns(notFoundTask); var collectionsController = new CollectionsV1Controller(_collectionServiceMock.Object); //get single meta data var actionResult = collectionsController.GetCustomFieldsCountAsync(118).Result; var response = actionResult as OkNegotiatedContentResult<int>; response.Content.Should().Be(0); //not found actionResult = collectionsController.GetCustomFieldsCountAsync(99).Result; actionResult.Should().BeOfType<NotFoundResult>(); _collectionServiceMock.VerifyAll(); }
public void GetCustomFieldTest() { var data = new List<CustomField>(); for (int i = 1; i < 3; i++) { var singleM = new CustomField(); singleM.Id = i; singleM.Key = "hometext"; singleM.Value = "true"; data.Add(singleM); } var metaResult = new ShopTimeResult<IList<CustomField>>(data); var notFoundResult = new ShopTimeResult<IList<CustomField>> {Error = new Error() {CodeNumber = 404}}; var emptyResult = new ShopTimeResult<IList<CustomField>>(); var metaTask = Task.FromResult<IResult<IList<CustomField>>>(metaResult); var notFoundTask = Task.FromResult<IResult<IList<CustomField>>>(notFoundResult); var emptyTask = Task.FromResult<IResult<IList<CustomField>>>(emptyResult); _collectionServiceMock.Setup(x => x.GetCustomFieldsAsync(3, true)).Returns(metaTask); _collectionServiceMock.Setup(x => x.GetCustomFieldsAsync(99, true)).Returns(notFoundTask); _collectionServiceMock.Setup(x => x.GetCustomFieldsAsync(58, true)).Returns(emptyTask); var collectionsController = new CollectionsV1Controller(_collectionServiceMock.Object); //get meta data var actionResult = collectionsController.GetCustomFieldsAsync(3).Result; var response = actionResult as OkNegotiatedContentResult<IList<CustomFieldModel>>; response.Content.Count.Should().Be(2); var metaModel = response.Content[1]; metaModel.Id.Should().Be(2); metaModel.Key.Should().Be("hometext"); //not found test actionResult = collectionsController.GetCustomFieldsAsync(99).Result; actionResult.Should().BeOfType<NotFoundResult>(); //empty test actionResult = collectionsController.GetCustomFieldsAsync(58).Result; var statusCoderesult = actionResult as StatusCodeResult; statusCoderesult.StatusCode.Should().Be(HttpStatusCode.NoContent); _collectionServiceMock.VerifyAll(); }
public void GetSingleMetaDataTest() { var singleM = new CustomField(); singleM.Id = 108; singleM.Key = "hometext"; singleM.Value = "true"; var metaResult = new ShopTimeResult<CustomField>(singleM); var badResult = new ShopTimeResult<CustomField> {Error = new Error()}; var notFoundResult = new ShopTimeResult<CustomField> {Error = new Error() {CodeNumber = 404}}; var metaTask = Task.FromResult<IResult<CustomField>>(metaResult); var badTask = Task.FromResult<IResult<CustomField>>(badResult); var notFoundTask = Task.FromResult<IResult<CustomField>>(notFoundResult); _collectionServiceMock.Setup(x => x.GetCustomFieldAsync(3, 108)).Returns(metaTask); _collectionServiceMock.Setup(x => x.GetCustomFieldAsync(99, 99)).Returns(notFoundTask); _collectionServiceMock.Setup(x => x.GetCustomFieldAsync(0, 0)).Returns(badTask); var collectionsController = new CollectionsV1Controller(_collectionServiceMock.Object); //single test var actionResult = collectionsController.GetCustomFieldAsync(3, 108).Result; var response = actionResult as OkNegotiatedContentResult<CustomFieldModel>; var metaModel = response.Content; metaModel.Id.Should().Be(singleM.Id); metaModel.Key.Should().Be(singleM.Key); metaModel.Value.Should().Be(singleM.Value); //notFound test actionResult = collectionsController.GetCustomFieldAsync(99, 99).Result; actionResult.Should().BeOfType<NotFoundResult>(); //badresult request actionResult = collectionsController.GetCustomFieldAsync(0, 0).Result; actionResult.Should().BeOfType<BadRequestResult>(); _collectionServiceMock.VerifyAll(); }
public async Task<IResult<int>> GetCustomFieldsCountAsync(int collectionId) { IResult<int> result = null; try { var count = _customFieldRepo.Table.Count(obj => obj.StoreId == _token.Store.Id && obj.Type == CustomFieldType.Collection && obj.ParentId == collectionId && obj.IsDeleted == false); result = new ShopTimeResult<int>(count); } catch (Exception ex) { result = new ShopTimeResult<int> { Error = new Error() { CodeNumber = 500, Message = ex.Message } }; } return result; }
public void CreateCollection() { var createCollectionResult = new ShopTimeResult<int>(13); var createTask = Task.FromResult<IResult<int>>(createCollectionResult); _collectionServiceMock.Setup(x => x.CreateCollectionAsync(It.IsAny<Collection>())).Returns(createTask); var collectionModel = new CollectionModel("DisplayName_Men", "men") { Image = new ImageModel() { Attachment = "base64", FileName = "abc.jpg"} }; var newCollection = Mapper.Map<Collection>(collectionModel); newCollection.Id = 13; var collectionsController = new CollectionsV1Controller(_collectionServiceMock.Object); var badResult = collectionsController.CreateCollectionAsync(collectionModel).Result; badResult.Should().BeOfType<BadRequestErrorMessageResult>("attachment is invalid."); collectionModel.Image.Attachment = new ImageHelper().GetBase64(); var actionResult = collectionsController.CreateCollectionAsync(collectionModel); var response = actionResult.Result as CreatedAtRouteNegotiatedContentResult<int>; response.Content.Should().Be(13); response.RouteName.Should().Be("GetCollectionById"); response.RouteValues["collectionId"].Should().Be(13); _collectionServiceMock.VerifyAll(); }
public async Task<IResult<IList<CustomField>>> GetCustomFieldsAsync(int collectionId, bool isTracking = true) { IResult<IList<CustomField>> result = null; try { var customFieldsQry = _customFieldRepo.Table.Where(obj => obj.StoreId == _token.Store.Id && obj.Type == CustomFieldType.Collection && obj.ParentId == collectionId && obj.IsDeleted == false); IList<CustomField> customFields = null; if (isTracking) { customFields = customFieldsQry.ToList(); } else { customFields = customFieldsQry.AsNoTracking().ToList(); } result = new ShopTimeResult<IList<CustomField>>(customFields); } catch (Exception ex) { result = new ShopTimeResult<IList<CustomField>> { Error = new Error() { CodeNumber = 500, Message = ex.Message } }; } return result; }
public void UpdateCollectionTest() { //arrange var collectionResult = new ShopTimeResult(); var collectionTask = Task.FromResult<IResult>(collectionResult); _collectionServiceMock.Setup(x => x.UpdateCollectionAsync(It.Is<Collection>(obj => obj.Id == 8))).Returns(collectionTask); var notFoundResult = new ShopTimeResult() { Error = new Error() { CodeNumber = 404 } }; var notFoundTask = Task.FromResult<IResult>(notFoundResult); _collectionServiceMock.Setup(x => x.UpdateCollectionAsync(It.Is<Collection>(obj => obj.Id == 99))).Returns(notFoundTask); var badResult = new ShopTimeResult<int> { Error = new Error() }; var badTask = Task.FromResult<IResult>(badResult); _collectionServiceMock.Setup(x => x.UpdateCollectionAsync(It.Is<Collection>(obj => obj.Id == 4))).Returns(badTask); var collectionsController = new CollectionsV1Controller(_collectionServiceMock.Object); //act var collectionModel = new CollectionModel("DisplayName_Men", "men"); collectionModel.Id = 323; var newCollection = Mapper.Map<Collection>(collectionModel); //ok result var actionResult = collectionsController.UpdateCollectionAsync(8, collectionModel).Result; actionResult.Should().BeOfType<OkResult>(); //result not found test actionResult = collectionsController.UpdateCollectionAsync(99, collectionModel).Result; actionResult.Should().BeOfType<NotFoundResult>(); //bad request actionResult = collectionsController.UpdateCollectionAsync(4, collectionModel).Result; actionResult.Should().BeOfType<BadRequestResult>(); }
public async Task<IResult<int>> CreateCustomFieldAsync(int collectionId, CustomField customField) { IResult<int> result = null; if (customField == null) { result = new ShopTimeResult<int>() { Error = new Error() { CodeNumber = 500, Message = "customField can't be null." }}; } try { customField.StoreId = _token.Store.Id; customField.Type = CustomFieldType.Collection; customField.ParentId = collectionId; if (customField.Key.IsNullOrWhiteSpace() == false && customField.Value.IsNullOrWhiteSpace() == false) { customField.Key = customField.Key.Trim(); customField.Value = customField.Value.Trim(); } customField.AuditInfo = new AuditInfo(_token); await _customFieldRepo.InsertAsync(customField).Complete(err => { if (err != null) { result = new ShopTimeResult<int>() {Error = err}; } result = new ShopTimeResult<int>(customField.Id); }); } catch (Exception ex) { result = new ShopTimeResult<int>() { Error = new Error() { CodeNumber = 500, Message = ex.Message } }; } return result; }
public void DeleteCollectionTest() { //arrange var get18Result = new ShopTimeResult<Collection>() { ResultValue = new Collection() {Id = 18}}; var get18Task = Task.FromResult<IResult<Collection>>(get18Result); _collectionServiceMock.Setup(x => x.GetCollectionAsync(18)).Returns(get18Task); var deleteResult = new ShopTimeResult(); var deleteTask = Task.FromResult<IResult>(deleteResult); _collectionServiceMock.Setup(x => x.DeleteCollectionAsync(It.Is<Collection>(obj => obj.Id == 18))).Returns(deleteTask); var getNotFoundResult = new ShopTimeResult<Collection>(); var getNotFoundTask = Task.FromResult<IResult<Collection>>(getNotFoundResult); _collectionServiceMock.Setup(x => x.GetCollectionAsync(99)).Returns(getNotFoundTask); var getBadResult = new ShopTimeResult<Collection>() { ResultValue = new Collection() { Id = 4 } }; var getBadTask = Task.FromResult<IResult<Collection>>(getBadResult); _collectionServiceMock.Setup(x => x.GetCollectionAsync(4)).Returns(getBadTask); var badResult = new ShopTimeResult<int> { Error = new Error() }; var badTask = Task.FromResult<IResult>(badResult); _collectionServiceMock.Setup(x => x.DeleteCollectionAsync(It.Is<Collection>(obj => obj.Id == 4))).Returns(badTask); var collectionsController = new CollectionsV1Controller(_collectionServiceMock.Object); //OK result var actionResult = collectionsController.DeleteCollectionAsync(18).Result; actionResult.Should().BeOfType<OkResult>(); //result not found test actionResult = collectionsController.DeleteCollectionAsync(99).Result; actionResult.Should().BeOfType<NotFoundResult>(); //bad request actionResult = collectionsController.DeleteCollectionAsync(4).Result; actionResult.Should().BeOfType<BadRequestResult>(); _collectionServiceMock.VerifyAll(); }
public async Task<IResult> UpdateCustomFieldAsync(CustomField customField) { IResult result = null; if (customField == null) { result = new ShopTimeResult() { Error = new Error() { CodeNumber = 500, Message = "customField can't be null." } }; return result; } if (customField.StoreId != _token.Store.Id) { result = new ShopTimeResult() { Error = new Error() { CodeNumber = 500, Message = "invalid operation." } }; return result; } try { customField.AuditInfo = new AuditInfo(_token); var updateResult = await _customFieldRepo.UpdateAsync(customField); if (updateResult.Error != null) { result = new ShopTimeResult() {Error = updateResult.Error}; return result; } result = new ShopTimeResult(); } catch (Exception ex) { result = new ShopTimeResult() { Error = new Error() { CodeNumber = 500, Message = ex.Message } }; } return result; }
public async Task<IResult> UpdateCollectionAsync(Collection collection) { IResult result = null; if (collection == null) { result = new ShopTimeResult() { Error = new Error() {CodeNumber = 500, Message = "collection can't be null."} }; return result; } var updateResult = await _collectionRepo.UpdateAsync(collection); if (updateResult.Error != null) { result = new ShopTimeResult<int> { Error = updateResult.Error }; return result; } result = new ShopTimeResult(); return result; }
public async Task<IResult<int>> CreateCollectionAsync(Collection target) { IResult<int> result = null; if (target == null) { result = new ShopTimeResult<int> { Error = new Error() {CodeNumber = 500, Message = "collection can't be null."} }; return result; } try { //resourceId can't be the same Collection oldCollection = null; await GetCollectionAsync(target.ResourceId).Complete((collection, err) => { if (err != null) { result = new ShopTimeResult<int> { Error = err }; return; } oldCollection = collection; }); if (oldCollection != null) { result = new ShopTimeResult<int> { Error = new Error() { CodeNumber = 500, Message = "resourceId is already existing." } }; return result; } //audit var auditInfo = new AuditInfo(_token); if (target.ResourceId.IsNullOrWhiteSpace()) { target.ResourceId = target.ResourceId.Trim(); } target.StoreId = _token.Store.Id; target.AuditInfo = auditInfo; //insert await _collectionRepo.InsertAsync(target).Complete(err => { if (err != null) { result = new ShopTimeResult<int> { Error = err }; return; } result = new ShopTimeResult<int>(target.Id); }); } catch (Exception ex) { result = new ShopTimeResult<int> { Error = new Error() { CodeNumber = 500, Message = ex.Message } }; } return result; }
public void GetCollectionsTest() { var collections = new List<Collection>(); for (var i = 0; i < 42; i++) { var collection = new Collection(); collection.Id = i; collection.ResourceId = string.Format("resouce_men{0}", i.ToString()); collection.DisplayName = string.Format("DisplayName_men{0}", i.ToString()); collection.Description = "Desc"; collection.Tags = new List<string>(){ "jean, polo"}; collection.IsVisible = true; //collection.Image = new Image() { Url = string.Format("http://cdn.example.com/collection/{0}/DisplayName_men.jpg", i.ToString()) }; collections.Add(collection); } var collectionsResult = new ShopTimeResult<IList<Collection>>(collections); var emptyResult = new ShopTimeResult<IList<Collection>>(); var notFoundResult = new ShopTimeResult<IList<Collection>> {Error = new Error() {CodeNumber = 404}}; var emptyTaskResult = Task.FromResult<IResult<IList<Collection>>>(emptyResult); var collectionsTaskResult = Task.FromResult<IResult<IList<Collection>>>(collectionsResult); var notFoundTaskResult = Task.FromResult<IResult<IList<Collection>>>(notFoundResult); //arrange _collectionServiceMock.Setup(x => x.GetCollectionsAsync(null, null, null, null, true)).Returns(emptyTaskResult); _collectionServiceMock.Setup(x => x.GetCollectionsAsync(43, null, null, null, true)).Returns(notFoundTaskResult); _collectionServiceMock.Setup(x => x.GetCollectionsAsync(1, null, null, null, true)).Returns(collectionsTaskResult); var collectionsController = new CollectionsV1Controller(_collectionServiceMock.Object); // return empty var actionResult = collectionsController.GetCollectionsAsync(); var statusCoderesult = actionResult.Result as StatusCodeResult; statusCoderesult.StatusCode.Should().Be(HttpStatusCode.NoContent); //return notFound var actionResult1 = collectionsController.GetCollectionsByProductIdAsync(43).Result; actionResult1.Should().BeOfType<NotFoundResult>(); //return collections var response = collectionsController.GetCollectionsByProductIdAsync(1).Result as OkNegotiatedContentResult<IList<CollectionModel>>; var model = response.Content; model.Count.Should().Be(collections.Count); _collectionServiceMock.VerifyAll(); }