From c90f5db3a8fa110a3905c4eb1ec2b26160426df6 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Sun, 28 Aug 2022 23:27:54 +0400 Subject: [PATCH] Added Remove() method. CAPTCHA word is capitalised in comments. In IsSolved() method of a DB a CAPTCHA's IsSolved method is now called within a return statement. --- pkg/captcha/db.go | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/pkg/captcha/db.go b/pkg/captcha/db.go index 8a4b264..9416c1d 100644 --- a/pkg/captcha/db.go +++ b/pkg/captcha/db.go @@ -34,6 +34,7 @@ type CaptchaDB interface { Image(id ID, style string) (*image.Image, error) Solve(id ID, answer Answer) (bool, error) IsSolved(id ID) (bool, error) + Remove(id ID) error cleanExpired() } @@ -92,12 +93,12 @@ func (cdb *InMemoryCaptchaDB) cleanExpired() { }() } -// GetExpiry returns time for how long captcha will last. +// GetExpiry returns time for how long CAPTCHA will last. func (cdb *InMemoryCaptchaDB) GetExpiry() time.Duration { return cdb.expireIn } -// Image returns image for a captcha. +// Image returns image for a CAPTCHA. func (cdb *InMemoryCaptchaDB) Image(id ID, style string) (*image.Image, error) { cdb.Lock() defer cdb.Unlock() @@ -108,7 +109,7 @@ func (cdb *InMemoryCaptchaDB) Image(id ID, style string) (*image.Image, error) { } // Solve compares given answer with a stored one and if failed -// deletes a captcha from database. +// deletes a CAPTCHA from database. func (cdb *InMemoryCaptchaDB) Solve(id ID, answer Answer) (bool, error) { cdb.Lock() defer cdb.Unlock() @@ -122,15 +123,25 @@ func (cdb *InMemoryCaptchaDB) Solve(id ID, answer Answer) (bool, error) { return false, errorNotFound } -// IsSolved checks if captcha was solved and removes it +// IsSolved checks if CAPTCHA was solved and removes it // from a database. func (cdb *InMemoryCaptchaDB) IsSolved(id ID) (bool, error) { cdb.Lock() defer cdb.Unlock() if c, ok := cdb.db[id]; ok { - ok = c.IsSolved() delete(cdb.db, id) - return ok, nil + return c.IsSolved(), nil } return false, errorNotFound } + +// Remove a CAPTCHA from a database. +func (cdb *InMemoryCaptchaDB) Remove(id ID) error { + cdb.Lock() + defer cdb.Unlock() + if _, ok := cdb.db[id]; ok { + delete(cdb.db, id) + return nil + } + return errorNotFound +}