Skip to content

Instantly share code, notes, and snippets.

View HamidMolareza's full-sized avatar

Hamid Molareza HamidMolareza

View GitHub Profile
@HamidMolareza
HamidMolareza / your_application.desktop
Created December 28, 2023 02:19
A template for Ubuntu desktop entry
# This is a sample .desktop file for Your Application
# Add this to: ~/.local/share/applications/your_application.desktop OR /usr/share/applications/your_application.desktop
[Desktop Entry]
# Version of the Desktop Entry Specification
Version=1.0
# Type of entry, should be "Application" for application launchers
Type=Application
@HamidMolareza
HamidMolareza / quera.sql
Last active November 19, 2023 09:16
CONCAT vs CONCAT_WS
# https://quera.org/college/8939/chapter/32512/lesson/108911/
-- Create a sample table
CREATE TABLE sample_table
(
id INT PRIMARY KEY AUTO_INCREMENT,
first_name VARCHAR(50),
last_name VARCHAR(50)
);
@HamidMolareza
HamidMolareza / read-lines.sh
Last active November 19, 2023 07:38
The script is a Bash program that reads and displays lines from a text file based on user-defined filters such as displaying the first N lines, last N lines, or a range of lines, with an added help message for usage guidance.
#!/bin/bash
# Function to display help information
show_help() {
echo "Usage:"
echo " $(basename "$0") [OPTIONS] FILE"
echo " pipeline_command | $(basename "$0") [OPTIONS]"
echo "Reads lines from a file based on the specified filter."
echo ""
echo "Options:"
@HamidMolareza
HamidMolareza / app.js
Last active November 12, 2023 21:56
stackoverflow-77470486
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
// Middleware to parse JSON in the query parameters
app.use(bodyParser.json());
app.get('/test', (req, res) => {
@HamidMolareza
HamidMolareza / PaginationModel.cs
Created October 4, 2023 07:09
Pagination for ASP MVC or Razor Pages
public class PaginationModel<T> : PageModel {
public int PageIndex { get; set; }
public int PageLimit { get; set; }
public int TotalItems { get; set; }
public List<T> Items { get; set; } = new();
public bool IsValidPage { get; set; }
public async Task LoadItemsAsync(IQueryable<T> source, int? page, int? limit) {
if (page < 1 || limit < 1) {
IsValidPage = false;
@HamidMolareza
HamidMolareza / ConnectionStrings.json
Last active October 16, 2024 17:25
Database Connection Strings
"ConnectionStrings": {
"Postgresql": "Host=localhost;Port=5432;Database=mydatabase;Username=myusername;Password=mypassword;",
"MySQL": "Server=localhost;Port=3306;Database=mydatabase;User=myusername;Password=mypassword;",
"SqlServer": "Server=localhost,1433;Database=DB_NAME;User Id=SA;Password=PASS;TrustServerCertificate=True;",
"Sqlite": "Data Source=app.db"
},
@HamidMolareza
HamidMolareza / ApiAuthorizeAttribute.cs
Last active October 4, 2023 06:49
Returns 401 error code when user is not authorized in ASP MVC or Razor Pages
public class ApiAuthorizeAttribute : TypeFilterAttribute {
public ApiAuthorizeAttribute() : base(typeof(ApiAuthorizeFilter)) {
}
}
public class ApiAuthorizeFilter : IAuthorizationFilter {
public void OnAuthorization(AuthorizationFilterContext context) {
if (context.HttpContext.User.Identity is not null &&
!context.HttpContext.User.Identity.IsAuthenticated) {
// Return a 401 Unauthorized status code
@HamidMolareza
HamidMolareza / launchSettings.json
Last active October 3, 2023 20:51
Run dotnet in Watching mode
"httpsWatch": {
"commandName": "Executable",
"executablePath": "dotnet",
"workingDirectory": "$(ProjectDir)",
"hotReloadEnabled": true,
"commandLineArgs": "watch run",
"applicationUrl": "http://localhost:5082",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
static int BinarySearch(IReadOnlyList<int> array, int target)
{
// Initialize left and right pointers for the binary search
int left = 0;
int right = array.Count - 1;
// Continue the search until the left pointer exceeds the right pointer
while (left <= right)
{
// Calculate the middle index
public void UpdateInstructorCourses(int[] selectedCourses, Instructor instructorToUpdate)
{
if (selectedCourses == null)
{
instructorToUpdate.CourseAssignments = new List<CourseAssignment>();
return;
}
var selectedCoursesHS = new HashSet<int>(selectedCourses);