Loading...
createShortUrl() và resolveAndRedirect()code -> longUrlInvalidUrlException: URL đầu vào saiNotFoundException: code không tồn tạiAlreadyExistsException: custom alias bị trùng (nếu có)StorageException: lỗi lưu/đọcQuan hệ:
UrlShortenerService phụ thuộc vào abstraction UrlRepository và ShortCodeGenerator (SOLID: DIP).UrlRepository lưu và trả UrlMapping.ShortCodeGenerator tạo code, service kiểm tra collision bằng repository.For each class, define the attributes (data) it will hold and the methods (functions) that operate on the attributes. Ensure they align with the object's responsibilities and adhere to the principle of encapsulation. Write your code in the code editor below.
UrlShortenerService.createShortUrl(longUrl)codeGenerator.generateCode()repository.exists(code) → nếu trùng thì retryUrlMapping(code, longUrl, now) và repository.save(mapping)https://sho.rt/{code}GET /{code}UrlShortenerService.getLongUrl(code)repository.findByCode(code)longUrl → handler trả HTTP 301/302 redirectNotFoundException → handler trả 404Expected errors
InvalidUrlException → trả 400InvalidCodeException → trả 400NotFoundException → trả 404StorageException → trả 500 (hoặc 503)Unexpected errors
StorageException để không “rò rỉ” chi tiết.