Example #1
0
        public async Task SetLikeAsync(int userId, int pictureId)
        {
            try
            {
                using (var connection = _factory.CreateConnection())
                {
                    await Connect(connection);

                    var like = new PictureLikes {
                        PictureId = pictureId, UserId = userId
                    };
                    if (!await uow.Pictures.IsLikeExist(connection, like))
                    {
                        await uow.Pictures.PushLike(connection, like);
                    }
                    else
                    {
                        throw new NotFoundException($"Лайк пользователя с id = {userId} для картины {pictureId} уже существует");
                    }
                }
            }
            catch (NotFoundException ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new DatabaseException("Не удалось добавить данные", ex.Message);
            }
        }
Example #2
0
        public async Task RemoveLikeAsync(int userId, int pictureId)
        {
            try
            {
                var like = new PictureLikes {
                    PictureId = pictureId, UserId = userId
                };
                using (var connection = _factory.CreateConnection())
                {
                    await Connect(connection);

                    if (await uow.Pictures.IsLikeExist(connection, like))
                    {
                        await connection.ExecuteAsync($"DELETE FROM {nameof(PictureLikes)} " +
                                                      $"WHERE {nameof(PictureLikes.UserId)} = @{nameof(userId)} AND {nameof(PictureLikes.PictureId)} = @{nameof(pictureId)}",
                                                      new { userId, pictureId });
                    }
                    else
                    {
                        throw new NotFoundException($"Не удалось найти лайк пользователя с id = {userId} для картины {pictureId}");
                    }
                }
            }
            catch (NotFoundException ex)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new DatabaseException("Не удалось удалить данные", ex.Message);
            }
        }
Example #3
0
 public Task <bool> IsLikeExist(DbConnection connection, PictureLikes like)
 {
     return(connection.QuerySingleAsync <bool>($"select iif(@{nameof(like.UserId)} = any (select [{nameof(PictureLikes.UserId)}] " +
                                               $"from [PictureLikes] where PictureId = @{nameof(like.PictureId)}),1,0)", like));
 }
Example #4
0
 public Task PushLike(DbConnection connection, PictureLikes like)
 {
     return(connection.ExecuteAsync(CreateQuery(like), like));
 }