Skip to content
This repository was archived by the owner on Sep 21, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion application/config/development/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
| environments.
|
*/
$config['base_url'] = 'https://'. DOMAIN;
$config['base_url'] = 'http://'. DOMAIN;

/*
|--------------------------------------------------------------------------
Expand Down
8 changes: 4 additions & 4 deletions application/config/development/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@
$query_builder = TRUE;

$db['default'] = array(
'dsn' => '',
'dsn' => 'mysql:host=localhost;dbname=test_task',
'hostname' => '127.0.0.1',
'username' => 'root',
'password' => '1234',
'username' => 'test_task',
'password' => 'test_task',
'database' => 'test_task',
'dbdriver' => 'mysqli',
'dbdriver' => 'pdomysql',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => FALSE,
Expand Down
179 changes: 164 additions & 15 deletions application/controllers/Main_page.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use Model\Boosterpack_model;
use Model\Login_model;
use Model\Post_model;
use Model\Transaction_info;
use Model\User_model;

/**
Expand All @@ -13,6 +15,8 @@
class Main_page extends MY_Controller
{

const LIKE_COST = 1;

public function __construct()
{
parent::__construct();
Expand Down Expand Up @@ -57,7 +61,20 @@ public function get_post($post_id){ // or can be $this->input->post('news_id') ,
}


public function comment($post_id,$message){ // or can be App::get_ci()->input->post('news_id') , but better for GET REQUEST USE THIS ( tests )
public function comment(){ // or can be App::get_ci()->input->post('news_id') , but better for GET REQUEST USE THIS ( tests )

$post_id = App::get_ci()->input->post('post_id');

// This isn't working (at least on nginx, php-fpm), have no time to investigate
// TODO: Investigate
$post_id = App::get_ci()->input->post('post_id');
$message = App::get_ci()->input->post('message');

// so do the bad way
$input = json_decode(App::get_ci()->input->raw_input_stream, true);

$post_id = @$input['post_id'];
$message = @$input['message'];

if (!User_model::is_logged()){
return $this->response_error(CI_Core::RESPONSE_GENERIC_NEED_AUTH);
Expand All @@ -76,32 +93,46 @@ public function comment($post_id,$message){ // or can be App::get_ci()->input->p
return $this->response_error(CI_Core::RESPONSE_GENERIC_NO_DATA);
}

// Todo: 2 nd task Comment
$post->comment();
// 2 nd task Comment
// TODO: Reply_to realization
$post->comment($message);

$posts = Post_model::preparation($post, 'full_info');
return $this->response_success(['post' => $posts]);
}


public function login($user_id)
public function login()
{
// Right now for tests we use from contriller

// This isn't working, have no time to investigate
// TODO: Investigate
$login = App::get_ci()->input->post('login');
$password = App::get_ci()->input->post('password');

// so do the bad way
$input = json_decode(App::get_ci()->input->raw_input_stream, true);

$login = @$input['login'];
$password = @$input['password'];

if (empty($login) || empty($password)){
return $this->response_error(CI_Core::RESPONSE_GENERIC_WRONG_PARAMS);
}

// But data from modal window sent by POST request. App::get_ci()->input... to get it.
// But data from modal window sent by POST request. App::get_ci()->input... to get it. - nope, you can't :D

$user = User_model::getByEmail($login);
if (!$user || !password_verify($password, $user->get_password())) {
return $this->response_error('invalid_credentials');
}

//Todo: 1 st task - Authorisation.
// password_hash('123321', PASSWORD_BCRYPT);

Login_model::start_session($user_id);
Login_model::start_session($user);

return $this->response_success(['user' => $user_id]);
return $this->response_success(['user' => $user->toArray()]);
}


Expand All @@ -112,19 +143,137 @@ public function logout()
}

public function add_money(){
// todo: 4th task add money to user logic
return $this->response_success(['amount' => rand(1,55)]); // Колво лайков под постом \ комментарием чтобы обновить . Сейчас рандомная заглушка
// 4th task add money to user logic

// This isn't working (at least on nginx, php-fpm), have no time to investigate
// TODO: Investigate
$sum = App::get_ci()->input->post('sum');

// so do the bad way
$input = json_decode(App::get_ci()->input->raw_input_stream, true);

$sum = floatval(@$input['sum']);

if (empty($sum) || $sum < 0.0){
return $this->response_error(CI_Core::RESPONSE_GENERIC_WRONG_PARAMS);
}

if (!User_model::is_logged()) {
return $this->response_error(CI_Core::RESPONSE_GENERIC_NEED_AUTH);
}

if (!User_model::get_user()->is_admin()) {
return $this->response_error(CI_Core::RESPONSE_GENERIC_NO_ACCESS);
}

User_model::get_user()->increase_wallet_balance(
$sum,
'Incoming transaction (topup)',
Transaction_info::TRANSACTION_TYPE_TOPUP
);

return $this->response_success(['amount' => User_model::get_user()->wallet_balance]); // Колво лайков под постом \ комментарием чтобы обновить . Сейчас рандомная заглушка
}

public function buy_boosterpack(){
// todo: 5th task add money to user logic
return $this->response_success(['amount' => rand(1,55)]); // Колво лайков под постом \ комментарием чтобы обновить . Сейчас рандомная заглушка
// 5th task add money to user logic

// This isn't working (at least on nginx, php-fpm), have no time to investigate
// TODO: Investigate
$id = App::get_ci()->input->post('id');

// so do the bad way
$input = json_decode(App::get_ci()->input->raw_input_stream, true);

$id = intval(@$input['id']);

if (!User_model::is_logged()){
return $this->response_error(CI_Core::RESPONSE_GENERIC_NEED_AUTH);
}

if (empty($id)){
return $this->response_error(CI_Core::RESPONSE_GENERIC_WRONG_PARAMS);
}

try {
$booster_pack = new Boosterpack_model($id);
} catch (EmeraldModelNoDataException $ex) {
return $this->response_error(CI_Core::RESPONSE_GENERIC_NO_DATA);
}

if (!User_model::get_user()->check_balance($booster_pack->get_price())) {
return $this->response_error(CI_Core::RESPONSE_GENERIC_INSUFFICIENT_BALANCE);
}

$topRandom = $booster_pack->get_price() + $booster_pack->get_bank();
$result = rand(1, $topRandom);
$bank = $booster_pack->get_price() - $result;
if ($bank < 0) {
$bank = 1;
}
$booster_pack->set_bank($bank);

User_model::get_user()->decrease_wallet_balance(
$booster_pack->get_price(),
sprintf('Purchase of booster pack #%d', $booster_pack->get_id()),
Transaction_info::TRANSACTION_TYPE_BOOSTER_PACK,
$booster_pack->get_id()
);

User_model::get_user()->increase_likes_balance(
$result,
sprintf('Opened booster pack #%d', $booster_pack->get_id()),
Transaction_info::TRANSACTION_TYPE_BOOSTER_PACK,
$booster_pack->get_id()
);

return $this->response_success(['amount' => $result]); // Колво лайков под постом \ комментарием чтобы обновить . Сейчас рандомная заглушка
}


public function like(){
// todo: 3rd task add like post\comment logic
return $this->response_success(['likes' => rand(1,55)]); // Колво лайков под постом \ комментарием чтобы обновить . Сейчас рандомная заглушка
public function like($type, $post_id){
// 3rd task add like post\comment logic

$post_id = intval($post_id);

if (!User_model::is_logged()){
return $this->response_error(CI_Core::RESPONSE_GENERIC_NEED_AUTH);
}

if (empty($post_id) || empty($type)){
return $this->response_error(CI_Core::RESPONSE_GENERIC_WRONG_PARAMS);
}

if (!User_model::get_user()->check_likes_balance(self::LIKE_COST)) {
return $this->response_error(CI_Core::RESPONSE_GENERIC_INSUFFICIENT_BALANCE);
}

$likes = null;
if ($type == 'post') {
try {
$post = new Post_model($post_id);
$post->increment_likes_count();
$likes = $post->get_likes_count();

// Decrease wallet balance on success
// We can use DB Transactions, but now i don't have a time
User_model::get_user()->decrease_likes_balance(
self::LIKE_COST,
sprintf('Like of post #%d', $post->get_id()),
Transaction_info::TRANSACTION_TYPE_LIKE,
$post->get_id()
);
} catch (EmeraldModelNoDataException $ex) {
return $this->response_error(CI_Core::RESPONSE_GENERIC_NO_DATA);
}
}

// TODO: Done
if ($type == 'comment') {
return $this->response_error(CI_Core::RESPONSE_GENERIC_UNAVAILABLE);
}

return $this->response_success(['likes' => $likes]); // Колво лайков под постом \ комментарием чтобы обновить . Сейчас рандомная заглушка
}

}
89 changes: 86 additions & 3 deletions application/models/Comment_model.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<?php

namespace Model;
use App;
use CI_Emerald_Model;
use Exception;
use stdClass;

/**
* Created by PhpStorm.
* User: mr.incognito
Expand All @@ -16,17 +22,21 @@ class Comment_model extends CI_Emerald_Model
/** @var int */
protected $assing_id;
/** @var string */
protected $text;
public $text;
/** @var int */
public $replies_count;

/** @var string */
protected $time_created;
public $time_created;
/** @var string */
protected $time_updated;

// generated
protected $comments;
protected $likes;
protected $user;
/** @var array */
public $replies;


/**
Expand All @@ -48,6 +58,25 @@ public function set_user_id(int $user_id)
return $this->save('user_id', $user_id);
}

/**
* @return int
*/
public function get_replies_count(): int
{
return $this->replies_count;
}

/**
* @param int $assing_id
*
* @return bool
*/
public function set_replies_count(int $repliesCount)
{
$this->replies_count = $repliesCount;
return $this->save('replies_count', $repliesCount);
}

/**
* @return int
*/
Expand Down Expand Up @@ -194,8 +223,60 @@ public function delete()
*/
public static function get_all_by_assign_id(int $assting_id)
{
$data = App::get_ci()->s->from(self::CLASS_TABLE)
->where(['assign_id' => $assting_id])
->where(['reply_to' => null])
->orderBy('time_created','ASC')
->many();

$ret = [];
foreach ($data as $i)
{
$comment = (new self())->set($i);
if ((int)$i['replies_count'] > 0) {
$replies = self::get_replies_tree($i['id']);
$comment->{'replies'} = self::build_tree($replies, $comment->id);
}
$ret[] = $comment;
}
return $ret;
}

function build_tree(array &$comments, $replyTo = 0) {
$final = [];

foreach ($comments as $comment) {
if ($comment->reply_to == $replyTo) {
$replies = self::build_tree($comments, $comment->id);
if ($replies) {
$comment->replies = $replies;
}
$final[] = $comment;
}
}
return $final;
}

/**
* @param int $postId
* @return self[]
* @throws Exception
*/
public static function get_replies_tree(int $postId)
{
$data = App::get_ci()->s->sql("WITH RECURSIVE nested_comments (id, reply_to, text, replies_count, personaname, lvl) AS
(
SELECT c.id, c.reply_to, c.text, c.replies_count, u.personaname, 0 lvl
FROM comment AS c
JOIN user AS u ON u.id = c.user_id
WHERE reply_to = ${postId}
UNION ALL
SELECT c.id, c.reply_to, c.text, c.replies_count, u.personaname, nc.lvl + 1
FROM nested_comments AS nc JOIN comment AS c
ON nc.id = c.reply_to
JOIN user AS u ON u.id = c.user_id
)SELECT * FROM nested_comments ORDER BY lvl")->many();

$data = App::get_ci()->s->from(self::CLASS_TABLE)->where(['assign_id' => $assting_id])->orderBy('time_created','ASC')->many();
$ret = [];
foreach ($data as $i)
{
Expand Down Expand Up @@ -235,6 +316,8 @@ private static function _preparation_full_info($data)

$o->id = $d->get_id();
$o->text = $d->get_text();
$o->replies = @$d->replies;
$o->replies_count = @$d->replies_count;

$o->user = User_model::preparation($d->get_user(),'main_page');

Expand Down
Loading