Exemple #1
0
        public async Task RemoveFromFavorite(RemoveFromFavorite.Request request, CancellationToken cancellationToken)
        {
            var userId = await _identityService.GetCurrentUserId(cancellationToken);

            if (string.IsNullOrEmpty(userId))
            {
                throw new UserNotFoundException("Пользователь не найден");
            }

            var advertisement = await _advertisementRepository.FindById(request.AdvertisementId, cancellationToken);

            if (advertisement == null)
            {
                throw new AdvertisementNotFoundException($"Объявление с Id = {request.AdvertisementId} не было найдено");
            }

            var favorite = await _repository.FindWhere(a => a.AdvertisementId == request.AdvertisementId && a.UserId == userId, cancellationToken);

            if (favorite == null)
            {
                throw new FavoriteNotFoundException($"Избранное объявление id = {request.AdvertisementId} у пользователя с id {userId} не было найдено");
            }

            await _repository.Delete(favorite, cancellationToken);
        }
        public IActionResult RemoveCourseFromFavorite([FromQuery] long?id)
        {
            var lang          = Request.Headers["language"].ToString();
            var errorMessages = new List <string>();

            try
            {
                if (id == null)
                {
                    errorMessages.Add(_translator.GetTranslation("ERROR", lang));
                    return(BadRequest(new { errors = errorMessages }));
                }

                var deletedFavorite = _favoriteRepository.Delete(id.Value);

                if (deletedFavorite == null)
                {
                    errorMessages.Add(_translator.GetTranslation("ERROR", lang));
                    return(BadRequest(new { errors = errorMessages }));
                }

                return(Ok(new { deletedFavoriteId = deletedFavorite.Id }));
            }
            catch
            {
                errorMessages.Add(_translator.GetTranslation("ERROR", lang));
                return(BadRequest(new { errors = errorMessages }));
            }
        }
        public async Task <IHttpActionResult> DeleteById(int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            var userId = authRepository.GetUserId();
            await favoriteRepository.Delete(id, userId);

            return(Ok());
        }
 public async Task <IActionResult> RemoveFavorite(int id)
 {
     try
     {
         var authorize = Barayand.Common.Services.TokenService.AuthorizeUser(Request);
         if (authorize < 1)
         {
             return(new JsonResult(ResponseModel.Error("Your login token has expired.")));
         }
         var Favs = ((List <FavoriteModel>)(await _favoritrepostory.GetAll()).Data).FirstOrDefault(x => x.F_UserId == authorize && x.F_EntityId == id);
         if (Favs == null)
         {
             return(new JsonResult(ResponseModel.Error("Your login token has expired.")));
         }
         return(new JsonResult(await _favoritrepostory.Delete(Favs)));
     }
     catch (Exception ex)
     {
         return(null);
     }
 }
Exemple #5
0
 public async Task <IActionResult> Delete(string id)
 {
     try
     {
         return(Ok(await _repository.Delete(id)));
     }
     catch (Exception exc)
     {
         _logger.LogError($"Error: {exc}");
         // transaction.Rollback();
         return(NotFound());
     }
 }
Exemple #6
0
        /// <summary>
        /// 删除收藏
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public ExecuteResult Destroy(FavoriteDestroyRequest request)
        {
            var favorEntity = _favoriteRepository.GetItem(request.FavoriteId);

            if (favorEntity == null)
            {
                return(new ExecuteResult {
                    StatusCode = StatusCode.ClientError, Message = "没有找到该产品"
                });
            }

            if (favorEntity.User_Id != request.AuthUid)
            {
                return(new ExecuteResult {
                    StatusCode = StatusCode.ClientError, Message = "您没有权限删除他人的收藏"
                });
            }

            _favoriteRepository.Delete(favorEntity);

            return(new ExecuteResult());
        }
        public async Task CreateOrDelete(string userId, string reviewId)
        {
            Guid userIdGuid   = Guid.Parse(userId);
            Guid reviewIdGuid = Guid.Parse(reviewId);

            Favorite insertedFavorite = await _favoriteRepository.GetByUserIdAndReviewId(userIdGuid, reviewIdGuid);

            if (insertedFavorite == null)
            {
                await ThrowIfReviewNotExists(reviewIdGuid);

                Favorite favorite = new Favorite(userIdGuid, reviewIdGuid);
                await _favoriteRepository.Create(favorite);

                await _favoriteRepository.Save();

                return;
            }

            _favoriteRepository.Delete(insertedFavorite);
            await _favoriteRepository.Save();
        }
Exemple #8
0
        public async Task <ResponseModel <Favorite> > DeleteFavoriteAsync(Guid clientId, Guid productId)
        {
            try
            {
                var targetFavorite = await _favoriteRepository.GetFavorite(clientId, productId);

                if (targetFavorite is null)
                {
                    return(new ResponseModel <Favorite>(true, new List <ErrorInfo>
                    {
                        new ErrorInfo(HttpStatusCode.NotFound, _messages.NoFavoriteFound,
                                      string.Format(_messages.NoFavoriteFoundDescription, clientId, productId))
                    }));
                }

                var deleted = await _favoriteRepository.Delete(targetFavorite);

                if (deleted == 0)
                {
                    return(new ResponseModel <Favorite>(true, new List <ErrorInfo>
                    {
                        new ErrorInfo(HttpStatusCode.Conflict, _messages.NoFavoriteChanged, _messages.NoFavoriteChangedDescription)
                    }));
                }

                return(new ResponseModel <Favorite>(targetFavorite));
            }
            catch (Exception ex)
            {
                var message = string.Format(_messages.InternalErrorDescription, ex.Message);
                _logger.LogError(ex, message);

                return(new ResponseModel <Favorite>(true, new List <ErrorInfo>
                {
                    new ErrorInfo(HttpStatusCode.InternalServerError, _messages.InternalError, message)
                }));
            }
        }
Exemple #9
0
 public void DeleteFavorite(FavoriteEntity favorite)
 {
     favoriteRepository.Delete(favorite.ToDalFavorite());
 }
Exemple #10
0
 public void Delete(int favoriteId)
 {
     _repo.Delete(favoriteId);
     _repo.SaveChanges();
 }
 public IActionResult Delete(int id)
 {
     _repo.Delete(id);
     return(NoContent());
 }