Created
October 23, 2021 12:16
-
-
Save dickyaryag6/110fbcf95cd95362283cd87c1461fe54 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package usecase | |
| import ( | |
| "errors" | |
| "gomocking/repository" | |
| "strconv" | |
| ) | |
| type Usecase struct { | |
| Database repository.DatabaseInterface | |
| } | |
| func New(usecase Usecase) Usecase { | |
| return usecase | |
| } | |
| func (u *Usecase) Addition(number1, number2 string) (int, error) { | |
| number1Int, err := convertStringToInt(number1) | |
| if err != nil { | |
| return 0, err | |
| } | |
| number2Int, err := convertStringToInt(number2) | |
| if err != nil { | |
| return 0, err | |
| } | |
| result := number1Int + number2Int | |
| err = u.Database.Insert(result) | |
| if err != nil { | |
| return 0, err | |
| } | |
| return result, nil | |
| } | |
| func convertStringToInt(s string) (int, error) { | |
| n, err := strconv.Atoi(s) | |
| if err != nil { | |
| return 0, errors.New("Input can't be converted to integer") | |
| } | |
| return n, nil | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment