Skip to content

Commit

Permalink
Release 1.1.6 (#48)
Browse files Browse the repository at this point in the history
T-14 Fix encoded url on server error:
  - Fix encoded url error
- Add decoder init, remove replacingPercentEncoding from
ProductsController
  - Add cache cleaning to Dockerfile
  - Add product available count scheduled restore job
  • Loading branch information
mayer1a authored Sep 8, 2024
2 parents 9422b60 + d396937 commit 2d4c4eb
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 4 deletions.
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# ================================
# Build image
# ================================
FROM swift:5.9.2-focal as build
FROM swift:5.9.2-focal AS build

RUN echo "[ BUILD IMAGE ]"

# Install OS updates and, if needed, sqlite3
RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
&& apt-get -q update \
&& apt-get -q dist-upgrade -y \
&& apt-get install -y libjemalloc-dev
&& apt-get install -y libjemalloc-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Set up a build area
WORKDIR /build
Expand Down Expand Up @@ -53,6 +55,7 @@ RUN export DEBIAN_FRONTEND=noninteractive DEBCONF_NONINTERACTIVE_SEEN=true \
&& apt-get -q update \
&& apt-get -q dist-upgrade -y \
&& apt-get -q install -y libjemalloc2 ca-certificates tzdata \
&& apt-get clean \
&& rm -r /var/lib/apt/lists/*

# Create a vapor user and group with /app as its home directory
Expand Down
5 changes: 5 additions & 0 deletions Sources/App/Configuration/configure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ struct Configuration {
.hourly()
.at(1)

app.queues.schedule(RestoreAvailableProductCountJob())
.weekly()
.on(.monday)
.at(00, 10, .am)

app.queues.add(PayOrderJob())

try app.queues.startInProcessJobs()
Expand Down
2 changes: 0 additions & 2 deletions Sources/App/Controllers/Products/ProductsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ struct ProductsController: RouteCollection {
}

@Sendable func getProducts(_ request: Request) async throws -> PaginationResponse<ProductDTO> {
request.url.query = request.url.query?.removingPercentEncoding

let page = try request.query.decode(PageRequest.self)
let filters = try? request.query.decode(FilterQueryRequest.self)
Expand Down Expand Up @@ -56,7 +55,6 @@ struct ProductsController: RouteCollection {
throw ErrorFactory.badRequest(.categoryIDRequired)
}

request.url.query = request.url.query?.removingPercentEncoding
let filters = try? request.query.decode(FilterQueryRequest.self)

return try await productsRepository.getFilters(for: categoryID, filters: filters)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// RestoreAvailableProductCountJob.swift
//
//
// Created by Artem Mayer on 08.09.2024.
//

import Vapor
import Fluent
import Queues
import FluentSQL

struct RestoreAvailableProductCountJob: AsyncScheduledJob {

static let rawSQLQuery: SQLQueryString = "SELECT * FROM AVAILABILITY_INFOS_COPY"

func run(context: QueueContext) async throws {

let sqlDatabase = context.application.db as! SQLDatabase
let _availabilityInfosCopy = try await sqlDatabase.raw(Self.rawSQLQuery)
.all(decodingFluent: AvailabilityInfo.self)
let infosCopy = Set<AvailabilityInfo>(_availabilityInfosCopy)

try await context.application.db.transaction { transaction in

let availabilityInfos = try await AvailabilityInfo.query(on: transaction).all()

try await availabilityInfos.asyncForEach { info in

if let updateCount = updatingCount(info: info, with: infosCopy) {
info.count = updateCount
try await info.update(on: transaction)
}
}
}
}

private func updatingCount(info: AvailabilityInfo, with infosCopy: Set<AvailabilityInfo>) -> Int? {
guard let infoCopyIndex = infosCopy.firstIndex(of: info) else { return nil }

let infoCopyCount = infosCopy[infoCopyIndex].count
return info.id == infosCopy[infoCopyIndex].id && info.count < infoCopyCount ? infoCopyCount : nil
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,16 @@ extension ProductVariant {
}

}

extension AvailabilityInfo: Hashable {


static func ==(lhs: AvailabilityInfo, rhs: AvailabilityInfo) -> Bool {
lhs.id == rhs.id
}

func hash(into hasher: inout Hasher) {
hasher.combine(id)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@
import Vapor

struct FilterQueryRequest: Content {

let filters: [String: [String]]?
let sort: String?

init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.sort = try container.decodeIfPresent(String.self, forKey: .sort)

var filters: [String: [String]] = [:]
if let filtersKey = container.allKeys.first(where: { $0.stringValue == "filters" }) {
let values = try container.decode([String: [String]].self, forKey: filtersKey)
values.forEach { key, values in
filters[key] = values.compactMap({ $0.removingPercentEncoding })
}
}

self.filters = filters
}

}

0 comments on commit 2d4c4eb

Please sign in to comment.