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.

This commit is contained in:
Alexander Andreev 2022-08-28 23:27:54 +04:00
parent 4a7dce3327
commit c90f5db3a8
Signed by: Arav
GPG Key ID: 0388CC8FAA51063F

View File

@ -34,6 +34,7 @@ type CaptchaDB interface {
Image(id ID, style string) (*image.Image, error) Image(id ID, style string) (*image.Image, error)
Solve(id ID, answer Answer) (bool, error) Solve(id ID, answer Answer) (bool, error)
IsSolved(id ID) (bool, error) IsSolved(id ID) (bool, error)
Remove(id ID) error
cleanExpired() 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 { func (cdb *InMemoryCaptchaDB) GetExpiry() time.Duration {
return cdb.expireIn 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) { func (cdb *InMemoryCaptchaDB) Image(id ID, style string) (*image.Image, error) {
cdb.Lock() cdb.Lock()
defer cdb.Unlock() 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 // 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) { func (cdb *InMemoryCaptchaDB) Solve(id ID, answer Answer) (bool, error) {
cdb.Lock() cdb.Lock()
defer cdb.Unlock() defer cdb.Unlock()
@ -122,15 +123,25 @@ func (cdb *InMemoryCaptchaDB) Solve(id ID, answer Answer) (bool, error) {
return false, errorNotFound return false, errorNotFound
} }
// IsSolved checks if captcha was solved and removes it // IsSolved checks if CAPTCHA was solved and removes it
// from a database. // from a database.
func (cdb *InMemoryCaptchaDB) IsSolved(id ID) (bool, error) { func (cdb *InMemoryCaptchaDB) IsSolved(id ID) (bool, error) {
cdb.Lock() cdb.Lock()
defer cdb.Unlock() defer cdb.Unlock()
if c, ok := cdb.db[id]; ok { if c, ok := cdb.db[id]; ok {
ok = c.IsSolved()
delete(cdb.db, id) delete(cdb.db, id)
return ok, nil return c.IsSolved(), nil
} }
return false, errorNotFound 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
}