forked from Yandex-Practicum/go-db-sql-final
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1e264ed
Showing
5 changed files
with
294 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
module delivery | ||
|
||
go 1.21 | ||
|
||
require ( | ||
github.com/dustin/go-humanize v1.0.1 // indirect | ||
github.com/google/uuid v1.3.0 // indirect | ||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect | ||
github.com/mattn/go-isatty v0.0.16 // indirect | ||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect | ||
golang.org/x/mod v0.3.0 // indirect | ||
golang.org/x/sys v0.9.0 // indirect | ||
golang.org/x/tools v0.0.0-20201124115921-2c860bdd6e78 // indirect | ||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect | ||
lukechampine.com/uint128 v1.2.0 // indirect | ||
modernc.org/cc/v3 v3.40.0 // indirect | ||
modernc.org/ccgo/v3 v3.16.13 // indirect | ||
modernc.org/libc v1.29.0 // indirect | ||
modernc.org/mathutil v1.6.0 // indirect | ||
modernc.org/memory v1.7.2 // indirect | ||
modernc.org/opt v0.1.3 // indirect | ||
modernc.org/sqlite v1.27.0 // indirect | ||
modernc.org/strutil v1.1.3 // indirect | ||
modernc.org/token v1.0.1 // indirect | ||
) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= | ||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= | ||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= | ||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs= | ||
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= | ||
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ= | ||
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= | ||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= | ||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= | ||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= | ||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= | ||
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4= | ||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= | ||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= | ||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/sys v0.9.0 h1:KS/R3tvhPqvJvwcKfnBHJwwthS11LRhmM5D59eEXa0s= | ||
golang.org/x/sys v0.9.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= | ||
golang.org/x/tools v0.0.0-20201124115921-2c860bdd6e78 h1:M8tBwCtWD/cZV9DZpFYRUgaymAYAr+aIUTWzDaM3uPs= | ||
golang.org/x/tools v0.0.0-20201124115921-2c860bdd6e78/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA= | ||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= | ||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
lukechampine.com/uint128 v1.2.0 h1:mBi/5l91vocEN8otkC5bDLhi2KdCticRiwbdB0O+rjI= | ||
lukechampine.com/uint128 v1.2.0/go.mod h1:c4eWIwlEGaxC/+H1VguhU4PHXNWDCDMUlWdIWl2j1gk= | ||
modernc.org/cc/v3 v3.40.0 h1:P3g79IUS/93SYhtoeaHW+kRCIrYaxJ27MFPv+7kaTOw= | ||
modernc.org/cc/v3 v3.40.0/go.mod h1:/bTg4dnWkSXowUO6ssQKnOV0yMVxDYNIsIrzqTFDGH0= | ||
modernc.org/ccgo/v3 v3.16.13 h1:Mkgdzl46i5F/CNR/Kj80Ri59hC8TKAhZrYSaqvkwzUw= | ||
modernc.org/ccgo/v3 v3.16.13/go.mod h1:2Quk+5YgpImhPjv2Qsob1DnZ/4som1lJTodubIcoUkY= | ||
modernc.org/libc v1.29.0 h1:tTFRFq69YKCF2QyGNuRUQxKBm1uZZLubf6Cjh/pVHXs= | ||
modernc.org/libc v1.29.0/go.mod h1:DaG/4Q3LRRdqpiLyP0C2m1B8ZMGkQ+cCgOIjEtQlYhQ= | ||
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4= | ||
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo= | ||
modernc.org/memory v1.7.2 h1:Klh90S215mmH8c9gO98QxQFsY+W451E8AnzjoE2ee1E= | ||
modernc.org/memory v1.7.2/go.mod h1:NO4NVCQy0N7ln+T9ngWqOQfi7ley4vpwvARR+Hjw95E= | ||
modernc.org/opt v0.1.3 h1:3XOZf2yznlhC+ibLltsDGzABUGVx8J6pnFMS3E4dcq4= | ||
modernc.org/opt v0.1.3/go.mod h1:WdSiB5evDcignE70guQKxYUl14mgWtbClRi5wmkkTX0= | ||
modernc.org/sqlite v1.27.0 h1:MpKAHoyYB7xqcwnUwkuD+npwEa0fojF0B5QRbN+auJ8= | ||
modernc.org/sqlite v1.27.0/go.mod h1:Qxpazz0zH8Z1xCFyi5GSL3FzbtZ3fvbjmywNogldEW0= | ||
modernc.org/strutil v1.1.3 h1:fNMm+oJklMGYfU9Ylcywl0CO5O6nTfaowNsh2wpPjzY= | ||
modernc.org/strutil v1.1.3/go.mod h1:MEHNA7PdEnEwLvspRMtWTNnp2nnyvMfkimT1NKNAGbw= | ||
modernc.org/token v1.0.1 h1:A3qvTqOwexpfZZeyI0FeGPDlSWX5pjZu9hF4lU+EKWg= | ||
modernc.org/token v1.0.1/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
_ "modernc.org/sqlite" | ||
) | ||
|
||
const ( | ||
ParcelStatusRegistered = "registered" | ||
ParcelStatusSent = "sent" | ||
ParcelStatusDelivered = "delivered" | ||
) | ||
|
||
type Parcel struct { | ||
Number int | ||
Client int | ||
Status string | ||
Address string | ||
CreatedAt string | ||
} | ||
|
||
type ParcelService struct { | ||
store ParcelStore | ||
} | ||
|
||
func NewParcelService(store ParcelStore) ParcelService { | ||
return ParcelService{store: store} | ||
} | ||
|
||
func (s ParcelService) Register(client int, address string) (Parcel, error) { | ||
parcel := Parcel{ | ||
Client: client, | ||
Status: ParcelStatusRegistered, | ||
Address: address, | ||
CreatedAt: time.Now().UTC().Format(time.RFC3339), | ||
} | ||
|
||
id, err := s.store.Add(parcel) | ||
if err != nil { | ||
return parcel, err | ||
} | ||
|
||
parcel.Number = id | ||
|
||
fmt.Printf("Новая посылка № %d на адрес %s от клиента с идентификатором %d зарегестрирована %s\n", | ||
parcel.Number, parcel.Address, parcel.Client, parcel.CreatedAt) | ||
|
||
return parcel, nil | ||
} | ||
|
||
func (s ParcelService) ClientsParcel(client int) error { | ||
list, err := s.store.GetByClient(client) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, item := range list { | ||
fmt.Printf("Посылка № %d на адрес %s от клиента с идентификатором %d зарегестрирована %s, статус %s\n", | ||
item.Number, item.Address, item.Client, item.CreatedAt, item.Status) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s ParcelService) NextStatus(number int) error { | ||
parcel, err := s.store.Get(number) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var nextStatus string | ||
switch parcel.Status { | ||
case ParcelStatusRegistered: | ||
nextStatus = ParcelStatusSent | ||
case ParcelStatusSent: | ||
nextStatus = ParcelStatusDelivered | ||
case ParcelStatusDelivered: | ||
return nil | ||
} | ||
|
||
fmt.Printf("У посылки %d новый статус: %s\n", number, nextStatus) | ||
|
||
return s.store.SetStatus(number, nextStatus) | ||
} | ||
|
||
func (s ParcelService) ChangeAddress(number int, address string) error { | ||
return s.store.SetAddress(number, address) | ||
} | ||
|
||
func (s ParcelService) Delete(number int) error { | ||
return s.store.Delete(number) | ||
} | ||
|
||
func main() { | ||
// настройте подключение к БД | ||
|
||
store := // создайте объект ParcelStore функцией NewParcelStore | ||
service := NewParcelService(store) | ||
|
||
// регистрация посылки | ||
client := 1 | ||
address := "Псков, д. Пушкина, ул. Колотушкина, д. 5" | ||
p, err := service.Register(client, address) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// изменение адреса | ||
newAddress := "Псков, д. Пушкина, ул. Колотушкина, д. 25" | ||
err = service.ChangeAddress(p.Number, newAddress) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// изменение статуса | ||
err = service.NextStatus(p.Number) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// вывод посылок клиента | ||
err = service.ClientsParcel(client) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// попытка удаления отправленной посылки | ||
err = service.Delete(p.Number) | ||
if err == nil { | ||
errMsg := "произошли нежелательные изменения: удалилась посылка со статусом, отличным от «registered»" | ||
panic(errors.New(errMsg)) | ||
} | ||
|
||
// регистрация новой посылки | ||
p, err = service.Register(client, address) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// удаление новой посылки | ||
err = service.Delete(p.Number) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
) | ||
|
||
type ParcelStore struct { | ||
db *sql.DB | ||
} | ||
|
||
func NewParcelStore(db *sql.DB) ParcelStore { | ||
return ParcelStore{db: db} | ||
} | ||
|
||
func (s ParcelStore) Add(p Parcel) (int, error) { | ||
// реализуйте добавление строки в таблицу parcel, используйте данные из переменной p | ||
|
||
// верните идентификатор последней добавленной записи | ||
return 0, nil | ||
} | ||
|
||
func (s ParcelStore) Get(number int) (Parcel, error) { | ||
// реализуйте чтение строки по заданному number | ||
// здесь из таблицы должна вернуться только одна строка | ||
|
||
// заполните объект Parcel данными из таблицы | ||
p := Parcel{} | ||
|
||
return p, nil | ||
} | ||
|
||
func (s ParcelStore) GetByClient(client int) ([]Parcel, error) { | ||
// реализуйте чтение строк из таблицы parcel по заданному client | ||
// здесь из таблицы может вернуться несколько строк | ||
|
||
// заполните срез Parcel данными из таблицы | ||
var res []Parcel | ||
|
||
return res, nil | ||
} | ||
|
||
func (s ParcelStore) SetStatus(number int, status string) error { | ||
// реализуйте обновление статуса в таблице parcel | ||
// верните ошибку, если ни одна из строк не была обновлена | ||
|
||
return nil | ||
} | ||
|
||
func (s ParcelStore) SetAddress(number int, address string) error { | ||
// реализуйте обновление адреса в таблице parcel | ||
// менять адрес можно только если значение статуса registered | ||
// верните ошибку, если ни одна из строк не была обновлена | ||
|
||
return nil | ||
} | ||
|
||
func (s ParcelStore) Delete(number int) error { | ||
// реализуйте удаление строки из таблицы parcel | ||
// удалять строку можно только если значение статуса registered | ||
// верните ошибку, если ни одна из строк не была удалена | ||
|
||
return nil | ||
} |