アプリ関連ニュース
Laravel Date Filtering
I would like to talk about the date filtering process I am using in laravel today.
If we want to select records that is created today. We can normally use raw queries or without raw queries like this.
1 2 3 4 5 |
//Raw Query Model::where(DB::raw("DATE(created_at) = '".date('Y-m-d')."'"))->get(); //Not Raw Query Model::where('created_at', '>=', date('Y-m-d').' 00:00:00'))->get(); |
In fact, we can do more neat and eloquent way in laravel like this.
1 |
Model::whereDate('created_at', '=', date('Y-m-d'))->get(); |
We can also use Carbon function instead of date(), result will still the same.
1 |
Model::whereDate('created_at', '=', Carbon::today()->toDateString())->get(); |
If you want to filter just not with full date, it’s totally fine. You can filter with day, month and year like this.
1 2 3 4 5 6 |
//Day Model::whereDay('created_at', '=', date('d'))->get(); //Month Model::whereMonth('created_at', '=', date('m'))->get(); //Year Model::whereYear('created_at', '=', date('Y'))->get(); |
There is also another method called whereBetween
if you want to filter by date range.
1 |
Model::whereBetween('created_at', [FROM_DATE, TO_DATE])->get(); |
By Yuuma
yuuma at 2021年02月22日 11:00:59
- 2021年2月17日
- Linux
ラズベリーパイOSをPC上で動かしてみました
PCにインストール可能なRaspberryPi用のOS(Raspberry Pi Desktop for PC)があります。
今回はこのOSを VirtualBox上の仮想PC にインストールしてみます。
tanaka at 2021年02月17日 10:00:33
- 2021年2月12日
- 技術情報
Dry code
Today I would like to make a short introduction about dry code.
What is dry code
There is a principle in programming called DRY, or Don’t Repeat Yourself. It usually means refactoring your code by taking something done multiple times and turning it into a loop or a function. The DRY code is easy to change, because you only have to make any changes in one place.
Advantages of DRY
Maintainability
The biggest benefit of using DRY is ease of maintenance. If the logic of checking permissions was repeated throughout the code, it is difficult to troubleshoot problems that occur in the repeated code. When you fix a problem in one, you can easily forget to fix the problem in other cases. Also, if you have to modify the logic, you have to copy and paste everywhere. By having non-repeating code, you just have to keep the code in one place. New bug and logic fixes can be made in one place instead of many. This leads to robust and reliable software.
Readability
Most of the time, the DRY code is more readable. This is not due to the DRY principle itself, but rather the extra effort that the developer put into the code to make it follow certain principles like DRY.
Re-use
DRY inherently promotes code reuse because we are merging 2 or more instances of repeating code into a single code block. Reusable code pays off in the long run as it speeds up development time.
Cost
If management needs to be convinced to spend more time improving code quality, this is it. More code costs more. More code requires more time to maintain and fix bugs. More time to develop and more mistakes leads to a very unhappy customer.
Tests
We are talking about unit testing and integration testing here, not manual testing. The more routes and functions you have to cover with the tests, the more code you will have to write for the tests. If your code doesn’t repeat, you just have to test one parent path.
By Yuuma.
yuuma at 2021年02月12日 07:27:55
AndroidのカメラサポートライブラリのCameraXを使ってみました
Androidのカメラ制御は
カメラが対応している解像度の取得や
プレビュー解像度の設定、画面回転など
いくつか面倒なステップを踏む必要がありますが
それらを吸収してくれる公式ライブラリがCameraXです。
ライブラリの使用方法はGoogleのCameraXのドキュメントに記載されていますが、
実際に動かしてみたコードを以下に記載します。
MainActivityにカメラプレビューを表示するだけの機能を持つアプリです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.camera.core.CameraSelector import androidx.camera.core.Preview import androidx.camera.lifecycle.ProcessCameraProvider import androidx.camera.view.PreviewView import androidx.core.content.ContextCompat import androidx.lifecycle.LifecycleOwner import com.google.common.util.concurrent.ListenableFuture class MainActivity : AppCompatActivity() { private lateinit var cameraProviderFuture: ListenableFuture<ProcessCameraProvider> private lateinit var previewView : PreviewView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // PreviewView previewView = findViewById(R.id.cameraPreview) // CameraProvider リクエスト cameraProviderFuture = ProcessCameraProvider.getInstance(this) // タスクを実行するエグゼキューター val executor = ContextCompat.getMainExecutor(this) // リスナー用タスク val listenerRunnable = Runnable { // ListenableFutureからCameraProviderを取得 val cameraProvider = cameraProviderFuture.get() // ライフサイクルにバインド -カメラの指定とプレビューの設定も行う bindToLifecycle(cameraProvider) } // CameraProvider リクエスト のリスナーをセット cameraProviderFuture.addListener(listenerRunnable, executor) } private fun bindToLifecycle(cameraProvider: ProcessCameraProvider) { // Previewを作成 val preview : Preview = Preview.Builder().build() // 背面カメラを選択 val cameraSelector : CameraSelector = CameraSelector.Builder() .requireLensFacing(CameraSelector.LENS_FACING_BACK) .build() // Previewとレイアウト上のPreviewViewを接続 preview.setSurfaceProvider(previewView.surfaceProvider) // CameraProvider をライフサイクルにバインド val camera = cameraProvider.bindToLifecycle(this as LifecycleOwner, cameraSelector, preview) } } |
Kotlinで書いているのもありますが、
ライブラリを使用しない時と比べて、ぐっと必要なコード量が減っています。
画像の撮影や、画像分析もライブラリに含まれているようです。
水曜担当:Tanaka
tanaka at 2021年02月10日 10:00:37
- 2021年2月09日
- 技術情報
Pseudocode
Pseudocode is an informal form of programming description that does not require any strict programming language syntax or underlying technology considerations. Used to create an outline or draft of a program. Pseudocode summarizes the flow of a program, but excludes the underlying details. Systems designers write pseudocode to ensure that programmers understand the requirements of a software project and align the code accordingly.
A Pseudocode is a prototype sample, model, or early release of a product created for the purpose of testing concepts and for learning purposes. They help us learn without fully implementing our solutions. When developing user interfaces for our applications, we have several prototypes before the final interface. Some examples of these are wire frames, graphic designs, and mockups.
This is the way how to write pseudo code.
- Organize your task sequence and write your pseudocode accordingly.
- Start with the declaration of a pseudocode that states the main goal or goal.
We have to know there are some KEYWORDS to be used in pseudo code.
These are for start and end of a program
BEGIN, END
These are for condition statements.
IF, ELSE, ENDIF, DO, WHILE, ENDWHILE, REPEAT, UNTIL, CASE
These are for user inputs.
INPUT, GET, READ , SET
These are for displaying results.
PRINT, SHOW
Here is some sample pseudo codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
//Conditional statement if if "1" print "this is 1" if "2" print "this is 2" //Case condition INPUT number CASE number of 1: PRINT "1" 2: PRINT "2" 3: PRINT "3" DEFAULT PRINT "0" ENDCASE //While Loop Set index to 0 WHILE index is GREATER THAN 1 PRINT index INCREASE index BY 1 END Function Function sample PRINT 'hello world' Endfunction |
There are still more keywords and syntax structures of writing pseudo codes but I think you will get the idea by reading this blog.
By Yuuma
yuuma at 2021年02月09日 07:07:22