//
// GetTourReviewsResponseJsonMapper.m
// Project
//
// Created by Ziv on 16/4/28.
//
//

#import "GetTourReviewsResponseJsonMapper.h"
#import "BeanUtils.h"
#import "CocoaUtils.h"
#import <RestKit/RKObjectMappingOperationDataSource.h>

@implementation GetTourReviewsResponseJsonMapper

- (instancetype)init {
	if (self = [super init]) {
		RKObjectMapping *reviewMapping = [RKObjectMapping mappingForClass:[TourReview class]];
		[reviewMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[TourReview class]]];

		RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass:[GetTourReviewsResponse class]];
		[responseMapping addAttributeMappingsFromArray:[BeanUtils keysForAccessibleSimplePropertyWithClass:[GetTourReviewsResponse class]]];
		[responseMapping addRelationshipMappingWithSourceKeyPath:@"reviews" mapping:reviewMapping];
		self.objectMapping = responseMapping;
	}
	return self;
}

- (NSError *)parseWithJSONObject:(id)jsonObject {
	self.response = [[GetTourReviewsResponse alloc] init];

	RKMappingOperation *mappingOperation = [[RKMappingOperation alloc] initWithSourceObject:jsonObject destinationObject:self.response mapping:self.objectMapping];
	RKObjectMappingOperationDataSource *mappingOperationDataSource = [[RKObjectMappingOperationDataSource alloc] init];
	mappingOperation.dataSource = mappingOperationDataSource;

	NSError *error = nil;
	[mappingOperation performMapping:&error];
	return error;
}

@end
