2

成长,就是不断向自己妥协的过程

 2 years ago
source link: https://naturalgao.github.io/2019/12/13/%E6%88%90%E9%95%BF%EF%BC%8C%E5%B0%B1%E6%98%AF%E4%B8%8D%E6%96%AD%E5%90%91%E8%87%AA%E5%B7%B1%E5%A6%A5%E5%8D%8F%E7%9A%84%E8%BF%87%E7%A8%8B/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

NaturalGao

HuiZhou, China

​ 在新东家实习已经快一个月了,环境还可以,工作也相对轻松。

​ 公司主要业务是电商 SAS 平台,开发部门算上我有 5 个人,3 个后端两个前端,我负责后端,算一个比较正规的小团队吧。

​ 实习期间呢,我全力负责 SAS 平台的三个模块后台的业务开发,分别是素材库、社区(帖子)、商学院(课程)。

​ 这些需求对久经沙场的我来说,自然不在话下,没到一个月就完成了初步的开发,接下来只需要与前端对接完就可以了。

但是这几天与 leader 发生了一些摩擦和争执,百般无奈下,我也只好向他妥协。

主要两个件事:

  1. 字段必填

    ​ 起初的时候我给课程设计了两个字段,一个是课程的开课和结束时间。(后面我仔细看了下需求,并不需要这两个字段)我数据库给这两个字段约束了”必填“ 就是 不能为 null。

    ​ 他可能迁移数据的时候,报了这两个字段没有默认值导致错误了吧~ 于是截图给了我,什么也不说。然后我就解释说,这两个字段设置必填才合理,不应该设置默认值。

    ​ 然后他给我的回应是:必填也要有默认值 (我一头雾水…)

  2. 代码冗余

    ​ 继 ”字段“ 事件后,他也对我写的一个接口进行了审查。 我写的一个 推荐产品的 Api 接口,可以根据不同的分类,筛选出可选的产品。

    ​ 举个例子:

    ​ 产品类型有:商品 和课程

    ​ 我需要根据这个类型 去对应的表拿可选的数据。后台添加数据的时候也有这个需求,于是我就写在了一个 Api,前端和后台都可以共用这个接口。

    ​ 于是,他就觉得这样不行,要求我拆分开来。 我问他原因呢,他又说不出来,我以为他不了解需求,就不断的向他讲述,最后没有用,结果自然闹得很僵。

    ​ 我们就这样僵了两三天吧,他就找我谈话了,说要我适应环境,把业务都写在一个 Function 里,因为昨天同事看了我的代码,跳来跳去的,看不懂~ ,

    ​ 我说我可以对我这块业务写个详细的文档,这也不行,无奈下,我也只好妥协。刚来不久,很多东西都不能硬性的去改变,也只能去适应他们的开发方式。

    ​ 接着,他对我的接口设计又有意见了。社区下的帖子和商学院里的课程都能评论,回复,点赞。

    于是我把这些各自写成一个接口,只需要传参数识别是课程还是帖子就行了。他要求我把这些拆分出来,就是说课程的评论和帖子的评论不能一个接口。百般的解析下,无用,我也只好再一次妥协。

修改后的对比

修改后的代码我真的不想看~ 感觉要被后面的人挖祖坟~~

就简单帖一个创建评论操作的对比吧~

  1. 创建评论操作入口
/**
* 创建评论
* @param CommentRequest $request
* @param $id
* @return mixed
*/
public function createComment(CommentRequest $request, $id)
{
//获取type,根据typpe 获取对应的query
$type = $request->input('type');
$query = BaseModel::getQuery($type);
$data = $request->except('type');

//检查对象是否存在
try {
$object = $query->findOrFail($id);
} catch (ModelNotFoundException $exception) {
return $this->failed('对象不存在');
}

try {
//把对象和数据传到创建评论操作 createCommentHandle
NewRetailCommonComment::createCommentHandle($object, $data);
return $this->success('评论成功');
} catch (\Exception $exception) {
return $this->failed($exception->getMessage());
}
}
  1. CommentRequest.php

Request 中传个类型和内容

<?php

namespace App\Http\Requests\Api;

use Illuminate\Foundation\Http\FormRequest;

class CommentRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'type' => 'required|in:post,course',
'content' => 'required|min:1'
];
}
}

  1. BaseModel.php

根据类型获取不同的实例

/**
* 获取不同的$Query
* @param $type
* @return bool|\Illuminate\Database\Eloquent\Builder
*/
public static function getQuery($type)
{
switch ($type):
case 'post':
return NewRetailCommunityPost::query();
break;
case 'course':
return NewRetailCourse::query();
break;
case 'comment':
return NewRetailCommonComment::query();
break;
case 'replies':
return NewRetailCommonCommentReplies::query();
break;
default:
return false;
endswitch;
}
  1. NewRetailCommonComment.php
/**
* 添加评论处理操作
* @param $object
* @param $data
* @return bool
* @throws \Exception
*/
public static function createCommentHandle($object, $data): bool
{
//判断对象的类型 获取实例
if ($object instanceof NewretailCommunityPost) {
$model = new NewretailCommunityPost();
} elseif ($object instanceof NewRetailCourse) {
$model = new NewRetailCourse();
}

//判断对象是否有效
$model::isEffective($object);

//评论功能是否开启
if (!$model::isComment()) {
throw new \Exception('评论功能还没有开启哦~~');
}

//是否需要审核
if (!$model::commentIsReview()) {
$data['status'] = CommunityPostEnum::ONE;
}

//返回创建所需的共用数据
$uuidAndCustomer = BaseModel::getCustomerAndUuid();
//数组合并
$data = array_merge($data, $uuidAndCustomer);
//创建评论
$object->comment()->save(new NewRetailCommonComment($data));
return true;
}

修改后的话,就是全部放一个 Function 里。实在没眼看~~

  1. 入口(创建文章评论)
/**
* 创建评论
* @param CommentRequest $request
* @param NewRetailCommunityPost $post
* @return mixed
*/
public function createComment(CommentRequest $request, NewRetailCommunityPost $post)
{
$data = $request->all();
try {
NewRetailCommonComment::createPostComment($data, $post);
return $this>success(ErrorCodeEnum::RETURN_ERROR_CODE_MSG[ErrorCodeEnum::ACTION_SUCCESS]);
} catch (\Exception $exception) {
return $this->failed($exception->getMessage());
}
}
  1. NewRetailCommonComment.php
/**
* 创建文章评论
* @param $data
* @param $post
* @return bool
* @throws \Exception
*/
public static function createPostComment($data, $post): bool
{
$model = new NewretailCommunityPost();

if ($post->is_del
|| $post->status != CommunityPostEnum::ONE
|| Auth::user()->customer_code != $post->customer_code) {
throw new \Exception(ErrorCodeEnum::RETURN_ERROR_CODE_MSG[ErrorCodeEnum::POST_NO_FOUND]);
}

/**
* 判断是否可以评论
*/
if (!$model::isComment()) {
throw new \Exception(ErrorCodeEnum::RETURN_ERROR_CODE_MSG[ErrorCodeEnum::NO_OPEN_COMMENT_PERMISSION]);
}

/**
* 判断是否需要审核
*/
if (!$model::commentIsReview()) {
$data['status'] = CommunityPostEnum::ONE;
}

$user = Auth::user();
$data = array_merge($data, [
'customer_code' => $user->customer_code,
'uuid' => $user->uuid
]);
$post->comment()->save(new NewRetailCommonComment($data));
return true;
}

创建课程评论 又需要 重新差不多的代码~~~

​ 我发这篇文章的本意并不是如何的抬高自己,贬低别人,我也不认为自己写得有多好,写一些业务代码,没什么值得骄傲的。通过这件事,我领悟到了,有时候有些事情,就算你很不喜欢,很不乐意,为了生活,也得适当的妥协。成长,就是不断向自己妥协的过程。

这种环境并不是我向往的,但是生活所迫,暂时任性不了,我还是会保持我的个性。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK