internal Result <AdEditInfo> GetAdEditInfo(UserInfo myUser, EditAdModel model) { var result = new AdEditInfo() { AdId = model.AdId, PlaceId = model.PlaceId, UserId = myUser.Id, Name = HttpUtility.HtmlDecode(model.Name), GenderId = model.GenderId, DateBorn = model.DateBorn, EyeColorId = model.EyeColorId, HairColorId = model.HairColorId, WeightGr = model.WeightGr, HeightCm = model.HeightCm, HairLengthId = model.HairLengthId, MainPicId = model.MainPicId, PicsIds = model.PicsIds, AlcoholId = model.AlcoholId, BodyTypeId = model.BodyTypeId, EducationLevelId = model.EducationLevelId, EthnicGroupId = model.EthnicGroupId, HasKids = model.HasKids, RelationshipStatusId = model.RelationshipStatusId, ReligionId = model.ReligionId, SmokingId = model.SmokingId, ZodiacSignId = model.ZodiacSignId }; return(Result <AdEditInfo> .NewSuccess(result)); }
public Result EditAd(AdEditInfo adInfo) { // Should be split into Mapping and Validation as separate steps var resultStorageAdInfo = GetValidatedStorageAdEditInfo(adInfo); if (!resultStorageAdInfo.Success) { return(Result.NewFailure(resultStorageAdInfo.ErrorMessage)); } var resultStoragePics = GetValidatedStoragePics(adInfo); if (!resultStoragePics.Success) { return(Result.NewFailure(resultStoragePics.ErrorMessage)); } var storageAdInfo = resultStorageAdInfo.Value; long adId = 0; if (adInfo.AdId.HasValue) { var resultUpdateAd = _adsStorage.UpdateAd(storageAdInfo); if (!resultUpdateAd.Success) { return(resultUpdateAd); } adId = adInfo.AdId.Value; } else { var resultNewAdId = _adsStorage.CreateAd(storageAdInfo); if (!resultNewAdId.Success) { return(Result.NewFailure(resultNewAdId.ErrorMessage)); } adId = resultNewAdId.Value; } // set mainPic in storage pics foreach (var sPic in resultStoragePics.Value) { sPic.IsPrimary = adInfo.MainPicId == sPic.AdMediaId; } var resultSetAdPics = _filesService.SetAdPics(adId: adId, pics: resultStoragePics.Value); if (!resultSetAdPics.Success) { return(resultSetAdPics); } _cache.InvalidateUserAds(userId: adInfo.UserId); return(Result.NewSuccess()); }
private Result <MediaModel[]> GetValidatedStoragePics(AdEditInfo adInfo) { if (!adInfo.PicsIds.Any()) { return(Result <MediaModel[]> .NewSuccess(Array.Empty <MediaModel>())); } var resultItems = new List <MediaModel>(); foreach (var picId in adInfo.PicsIds) { var resultPic = _filesService.GetPic(id: picId); if (!resultPic.Success) { return(Result <MediaModel[]> .NewFailure($"Can't find pic by ID: '{picId}'.")); } resultItems.Add(resultPic.Value); } return(Result <MediaModel[]> .NewSuccess(resultItems.ToArray())); }
private Result <StorageAdEditInfo> GetValidatedStorageAdEditInfo(AdEditInfo ad) { if (ad == null) { Result <StorageAdEditInfo> .NewFailure($"{nameof(ad)} is NULL."); } var result = new StorageAdEditInfo(); result.AdId = ad.AdId; // UserId; //var resultUserAds = GetUserAds(ad.UserId); //if (!resultUserAds.Success) // return Result<StorageAdEditInfo>.NewFailure($"Failed to receive user ads by user id: '{ad.UserId}'."); //if (resultUserAds.Value.Any()) // return Result<StorageAdEditInfo>.NewFailure("User already has an ad."); result.UserId = ad.UserId; //PlaceId; var maybePlace = _placesService.GetPlace(ad.PlaceId); if (!maybePlace.Success) { return(Result <StorageAdEditInfo> .NewFailure($"Can't find place by id: {ad.PlaceId}.")); } if (!maybePlace.Value.IsEnabled) { return(Result <StorageAdEditInfo> .NewFailure($"Place '{maybePlace.Value.PlaceCode}' is not enabled.")); } if (maybePlace.Value.PlaceType != PlaceType.City) { return(Result <StorageAdEditInfo> .NewFailure($"Place '{maybePlace.Value.PlaceCode}' is not of type '{PlaceType.City}'.")); } result.PlaceId = ad.PlaceId; //Name; result.Name = ad.Name; //DateBorn; if (ad.DateBorn < DateTime.Now.AddYears(-80) || ad.DateBorn > DateTime.Now.AddYears(-18)) { return(Result <StorageAdEditInfo> .NewFailure($"{nameof(ad.DateBorn)} is invalid. (Too old or too young.")); } result.DateBorn = ad.DateBorn; //GenderId; if (!ObjectIsEnabledOfType(objectTypeCode: ObjectTypeCodes.Gender, objectId: ad.GenderId)) { return(Result <StorageAdEditInfo> .NewFailure($"{nameof(ad.GenderId)} did not pass validation.")); } result.GenderId = ad.GenderId; //HeightCm; if (ad.HeightCm.HasValue && (ad.HeightCm.Value < 110 || ad.HeightCm > 250)) { return(Result <StorageAdEditInfo> .NewFailure($"Height is invalid.")); } result.HeightCm = ad.HeightCm; //WeightGr; if (ad.WeightGr.HasValue && (ad.WeightGr < 30 * 1000 || ad.WeightGr > 200 * 1000)) { return(Result <StorageAdEditInfo> .NewFailure($"Weight is invalid.")); } result.WeightGr = ad.WeightGr; //EyeColorId; if (ad.EyeColorId.HasValue && !ObjectIsEnabledOfType(objectTypeCode: ObjectTypeCodes.EyeColor, objectId: ad.EyeColorId.Value)) { return(Result <StorageAdEditInfo> .NewFailure($"{nameof(ad.EyeColorId)} did not pass validation.")); } result.EyeColorId = ad.EyeColorId; //HairColorId; if (ad.HairColorId.HasValue && !ObjectIsEnabledOfType(objectTypeCode: ObjectTypeCodes.HairColor, objectId: ad.HairColorId.Value)) { return(Result <StorageAdEditInfo> .NewFailure($"{nameof(ad.HairColorId)} did not pass validation.")); } result.HairColorId = ad.HairColorId; //HairLengthId; if (ad.HairLengthId.HasValue && !ObjectIsEnabledOfType(objectTypeCode: ObjectTypeCodes.HairLength, objectId: ad.HairLengthId.Value)) { return(Result <StorageAdEditInfo> .NewFailure($"{nameof(ad.HairLengthId)} did not pass validation.")); } result.HairLengthId = ad.HairLengthId; return(Result <StorageAdEditInfo> .NewSuccess(result)); }