public virtual IActionResult UsersCurrentFavouritesIdDeletePost([FromRoute] int id)
        {
            // get the current user id
            string userId = _context.SmUserId;

            // not found
            if (userId == null)
            {
                return(new NotFoundObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration))));
            }

            bool exists = _context.HetUserFavourite
                          .Where(x => x.User.SmUserId == userId)
                          .Any(a => a.UserFavouriteId == id);

            // not found
            if (!exists)
            {
                return(new NotFoundObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration))));
            }

            // delete favourite
            HetUserFavourite item = _context.HetUserFavourite.First(a => a.UserFavouriteId == id);

            _context.HetUserFavourite.Remove(item);

            // save the changes
            _context.SaveChanges();

            return(new ObjectResult(new HetsResponse(item)));
        }
        private IActionResult UpdateFavourite(HetUserFavourite item)
        {
            item.User = null;

            // get the current user id
            string userId = _context.SmUserId;

            // not found
            if (userId == null)
            {
                return(new NotFoundObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration))));
            }

            // get initial results - must be limited to user's district
            int?districtId = UserAccountHelper.GetUsersDistrictId(_context, _httpContext);

            // get user record
            bool userExists = _context.HetUser.Any(a => a.SmUserId == userId);

            if (!userExists)
            {
                return(new NotFoundObjectResult(new HetsResponse("HETS-01", ErrorViewModel.GetDescription("HETS-01", _configuration))));
            }

            HetUser user = _context.HetUser.AsNoTracking()
                           .First(a => a.SmUserId == userId);

            // get favourites
            bool exists = _context.HetUserFavourite.Any(a => a.UserFavouriteId == item.UserFavouriteId);

            HetUserFavourite favourite;

            if (exists)
            {
                favourite = _context.HetUserFavourite
                            .First(a => a.UserFavouriteId == item.UserFavouriteId);

                favourite.ConcurrencyControlNumber = item.ConcurrencyControlNumber;
                favourite.Value     = item.Value;
                favourite.Name      = item.Name;
                favourite.IsDefault = item.IsDefault;

                _context.HetUserFavourite.Update(favourite);
            }
            else
            {
                favourite = new HetUserFavourite
                {
                    UserId     = user.UserId,
                    DistrictId = districtId,
                    Type       = item.Type,
                    Value      = item.Value,
                    Name       = item.Name,
                    IsDefault  = item.IsDefault
                };

                _context.HetUserFavourite.Add(favourite);
            }

            // save the changes
            _context.SaveChanges();

            int favouriteId = favourite.UserFavouriteId;

            // get record and return
            favourite = _context.HetUserFavourite.AsNoTracking()
                        .First(x => x.UserFavouriteId == favouriteId);

            return(new ObjectResult(new HetsResponse(favourite)));
        }
 public virtual IActionResult UsersCurrentFavouritesPut([FromBody] HetUserFavourite item)
 {
     return(UpdateFavourite(item));
 }