private async Task <IActionResult> SaveProfile <TVm, TE>(ViewModelPatchBag <TVm> mpb, IRepository <TE> repo)
            where TVm : BaseProfileViewModel
            where TE : BaseProfileEntity
        {
            if (mpb.Model == null)
            {
                throw new KnownException("Invalid request object.");
            }

            var existing = await repo.FindOne(p => p.User == CurrentUser);

            TE e;

            if (existing == null)
            {
                e      = Mapper.Map <TE>(mpb.Model);
                e.User = CurrentUser;
                e      = await repo.Add(e);
            }
            else
            {
                repo.ChainQueryable(q => q.Include(p => p.Address));
                var eub = Mapper.Map <EntityPatchBag <TE> >(mpb);
                eub.Id = existing.Id;
                e      = await repo.Patch(eub);
            }

            return(Ok(Mapper.Map <TVm>(e)));
        }
 protected void ClearForbiddenPatchFields <T>(ViewModelPatchBag <T> vmpb) where T : ViewModel
 {
     // find properties that are about to be patched but them cannot be updated (CantPatchPropertyFunc)
     // set them to false so the Repo won't touch them
     foreach (var pn in vmpb.Props.Keys.ToList().Where(pn => vmpb.Props[pn]).Where(pn =>
                                                                                   CantPatchPropertyFunc(typeof(T).GetProperty(pn,
                                                                                                                               BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance))))
     {
         vmpb.Props[pn] = false;
     }
 }
        public override async Task <IActionResult> Patch(ViewModelPatchBag <ProductViewModel> vmub)
        {
            var isHis = await Repo.Any(p => p.Id == vmub.Id && p.Seller == CurrentSellerProfile);

            if (!isHis)
            {
                throw new KnownException("Can't patch this product. It's not yours.");
            }

            return(await base.Patch(vmub));
        }
        public async Task <IActionResult> SaveAddress([FromBody] ViewModelPatchBag <AddressViewModel> address)
        {
            if (await UserManager.IsInRoleAsync(CurrentUser, "Seller"))
            {
                return(await SaveAddress(address, SellersRepo));
            }

            if (await UserManager.IsInRoleAsync(CurrentUser, "Buyer"))
            {
                return(await SaveAddress(address, BuyersRepo));
            }

            return(BadRequest());
        }
        public virtual async Task <IActionResult> Patch([FromBody] ViewModelPatchBag <TVm> vmub)
        {
            if (!await Repo.Exists(vmub.Id))
            {
                return(NotFound());
            }

            ClearForbiddenPatchFields(vmub);

            var eub = Mapper.Map <EntityPatchBag <TE> >(vmub);
            var e   = await Repo.Patch(eub);

            var vm = Map(e);

            return(Ok(vm));
        }
        private async Task <IActionResult> SaveAddress <TE>(ViewModelPatchBag <AddressViewModel> mpb,
                                                            IRepository <TE> repo)
            where TE : BaseProfileEntity
        {
            repo.ChainQueryable(q => q.Include(p => p.Address));
            var profile = await repo.FindOne(p => p.User == CurrentUser);

            AddressEntity e;

            if (profile == null)
            {
                throw new KnownException("You must complete your profile first.");
            }

            if (profile.Address == null)
            {
                e = Mapper.Map <AddressEntity>(mpb.Model);
                e = await AddressesRepo.Add(e);

                profile.Address = e;
                await DataLayer.SaveChangesAsync();
            }
            else
            {
                repo.ChainQueryable(q => q.Include(p => p.Address));
                var eub = Mapper.Map <EntityPatchBag <AddressEntity> >(mpb);
                eub.Id = profile.Address.Id;
                if (eub.PropertiesToUpdate["location"])
                {
                    eub.PropertiesToUpdate["locationLat"] = eub.PropertiesToUpdate["locationLng"] = true;
                }

                e = await AddressesRepo.Patch(eub);
            }

            return(Ok(Mapper.Map <AddressViewModel>(e)));
        }
 public override Task <IActionResult> Patch(ViewModelPatchBag <SubscriberViewModel> vmub)
 {
     return(base.Patch(vmub));
 }
 public async Task <IActionResult> SaveBuyerProfile([FromBody] ViewModelPatchBag <BuyerProfileViewModel> model)
 {
     return(await SaveProfile(model, BuyersRepo));
 }
 public override Task <IActionResult> Patch(ViewModelPatchBag <ReferenceTrackViewModel> vmub)
 {
     return(new Task <IActionResult>(Ok));
 }