Skip to content

Commit a76bde9

Browse files
author
Amador Quispe
committed
#26 - GO
1 parent 9077936 commit a76bde9

File tree

1 file changed

+325
-0
lines changed

1 file changed

+325
-0
lines changed
Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,325 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
//APLICANDO SRP
8+
9+
type Employee struct {
10+
id int
11+
name string
12+
email string
13+
}
14+
15+
func (e *Employee) GetId() int {
16+
return e.id
17+
}
18+
19+
func (e *Employee) GetName() string {
20+
return e.name
21+
}
22+
23+
func (e *Employee) GetEmail() string {
24+
return e.email
25+
}
26+
27+
// Repository
28+
type EmployeeRepository struct {
29+
// Database connection or other storage-related fields
30+
}
31+
32+
func (repo *EmployeeRepository) Save(employee *Employee) error {
33+
// Save user to the database
34+
fmt.Println("saving data...", employee)
35+
return nil
36+
}
37+
38+
// Service
39+
type EmployeeService struct {
40+
//Repositories and other services
41+
}
42+
43+
func (svc *EmployeeService) Notification(employee *Employee) error {
44+
fmt.Println("Sending notification", employee.GetEmail())
45+
return nil
46+
}
47+
48+
//EXTRA
49+
// APLICANDO SRP
50+
51+
type User struct {
52+
doi string
53+
name string
54+
email string
55+
}
56+
57+
type UserRepository struct {
58+
users []User
59+
}
60+
61+
func (userRepo *UserRepository) Register(user User) {
62+
userRepo.users = append(userRepo.users, user)
63+
fmt.Println("usuario registrado")
64+
}
65+
66+
func (userRepo *UserRepository) FindByDoi(doi string) *User {
67+
for i, user := range userRepo.users {
68+
if user.doi == doi {
69+
return &userRepo.users[i]
70+
}
71+
}
72+
return nil
73+
}
74+
75+
type Book struct {
76+
title string
77+
author string
78+
copies int
79+
}
80+
81+
type BookRepository struct {
82+
books []Book
83+
}
84+
85+
func (bookRepo *BookRepository) Register(book Book) {
86+
bookRepo.books = append(bookRepo.books, book)
87+
fmt.Println("libro registrado")
88+
}
89+
90+
func (bookRepo *BookRepository) FindByTitle(title string) *Book {
91+
for i, book := range bookRepo.books {
92+
if book.title == title {
93+
return &bookRepo.books[i]
94+
}
95+
}
96+
return nil
97+
}
98+
99+
func (bookRepo *BookRepository) IncreaseCopies(title string, delta int) {
100+
book := bookRepo.FindByTitle(title)
101+
book.copies += delta
102+
}
103+
104+
type LoanRepository struct {
105+
loanBooks map[string][]string
106+
}
107+
108+
func (loanRepo *LoanRepository) LoanBook(userDoi, bookTitle string) {
109+
if v, ok := loanRepo.loanBooks[userDoi]; ok {
110+
v = append(v, bookTitle)
111+
loanRepo.loanBooks[userDoi] = v
112+
} else {
113+
loanRepo.loanBooks[userDoi] = []string{bookTitle}
114+
}
115+
fmt.Println("préstamo del libro registrado")
116+
}
117+
118+
func (loanRepo *LoanRepository) ReturnBook(userDoi, bookTitle string) {
119+
for key, values := range loanRepo.loanBooks {
120+
if key == userDoi {
121+
var newList []string
122+
for _, v := range values {
123+
if v != bookTitle {
124+
newList = append(newList, v)
125+
}
126+
}
127+
loanRepo.loanBooks[userDoi] = newList
128+
fmt.Println("Libro devuelto correctamente")
129+
break
130+
}
131+
}
132+
}
133+
134+
type Library struct {
135+
bookRepo BookRepository
136+
userRepo UserRepository
137+
loanRepo LoanRepository
138+
}
139+
140+
func (lib *Library) CreateBook(book Book) {
141+
lib.bookRepo.Register(book)
142+
}
143+
144+
func (lib *Library) CreateUser(user User) {
145+
lib.userRepo.Register(user)
146+
}
147+
148+
func (lib *Library) RegisterLoanBook(userDoi, bookTitle string) {
149+
user := lib.userRepo.FindByDoi(userDoi)
150+
book := lib.bookRepo.FindByTitle(bookTitle)
151+
if user == nil && book == nil {
152+
fmt.Println("usuario o book no está registrado")
153+
}
154+
lib.loanRepo.LoanBook(userDoi, bookTitle)
155+
lib.bookRepo.IncreaseCopies(bookTitle, -1)
156+
}
157+
158+
func (lib *Library) RegisterReturnBook(userDoi, bookTitle string) {
159+
user := lib.userRepo.FindByDoi(userDoi)
160+
book := lib.bookRepo.FindByTitle(bookTitle)
161+
if user == nil && book == nil {
162+
fmt.Println("usuario o book no está registrado")
163+
}
164+
lib.loanRepo.ReturnBook(userDoi, bookTitle)
165+
lib.bookRepo.IncreaseCopies(bookTitle, +1)
166+
167+
}
168+
169+
func main() {
170+
/*library := Library{
171+
books: []Book{},
172+
users: []User{},
173+
loanBooks: make(map[string][]string),
174+
}
175+
library.AddUser(&User{doi: "44557855", name: "Amador", email: "aquispe@gmail.com"})
176+
library.AddBook(&Book{title: "clean code", author: "Robert C. Martin", copies: 4})
177+
library.LoanBook("clean code", "44557855")
178+
fmt.Println(library.books)
179+
library.ReturnBook("clean code", "44557855")
180+
fmt.Println(library.books)*/
181+
bookRepo := BookRepository{
182+
books: []Book{},
183+
}
184+
185+
userRepo := UserRepository{
186+
users: []User{},
187+
}
188+
189+
loanRepo := LoanRepository{
190+
loanBooks: make(map[string][]string),
191+
}
192+
193+
library := Library{
194+
bookRepo: bookRepo,
195+
userRepo: userRepo,
196+
loanRepo: loanRepo,
197+
}
198+
199+
library.CreateBook(Book{title: "clean code", author: "Robert C. Martin", copies: 4})
200+
library.CreateBook(Book{title: "clean architecture", author: "Robert C. Martin", copies: 4})
201+
202+
library.CreateUser(User{doi: "44557855", name: "Amador", email: "aquispe@gmail.com"})
203+
library.CreateUser(User{doi: "44775588", name: "Alex", email: "alex@gmail.com"})
204+
205+
library.RegisterLoanBook("44557855", "clean code")
206+
fmt.Println(library.bookRepo.books)
207+
208+
library.RegisterReturnBook("44557855", "clean code")
209+
fmt.Println(library.bookRepo.books)
210+
211+
}
212+
213+
//Viola SRP
214+
/*type Employee struct {
215+
id int
216+
name string
217+
email string
218+
}
219+
220+
func (e *Employee) GetName() string {
221+
return e.name
222+
}
223+
224+
func (e *Employee) GetEmail() string {
225+
return e.email
226+
}
227+
228+
func (e *Employee) SaveToDatabase() {
229+
fmt.Println("saving data in database!! ", e)
230+
}
231+
232+
func (e *Employee) SendNotification() {
233+
fmt.Println("sending notification ", e.email)
234+
}*/
235+
236+
//EXTRA
237+
//Viola SRP
238+
/*
239+
type Book struct {
240+
title string
241+
author string
242+
copies uint
243+
}
244+
245+
type User struct {
246+
doi string
247+
name string
248+
email string
249+
}
250+
251+
type Library struct {
252+
books []Book
253+
users []User
254+
loanBooks map[string][]string
255+
}
256+
257+
func (l *Library) AddUser(user *User) {
258+
l.users = append(l.users, *user)
259+
}
260+
261+
func (l *Library) AddBook(book *Book) {
262+
l.books = append(l.books, *book)
263+
}
264+
265+
func (l *Library) LoanBook(bookTitle, userDoi string) {
266+
var userFound *User
267+
var bookFound *Book
268+
for i, user := range l.users {
269+
if user.doi == userDoi {
270+
userFound = &l.users[i]
271+
break
272+
}
273+
}
274+
275+
for i, book := range l.books {
276+
if book.title == bookTitle {
277+
bookFound = &l.books[i]
278+
break
279+
}
280+
}
281+
282+
if userFound == nil || bookFound == nil {
283+
fmt.Println("usuario o libro no está registrada")
284+
return
285+
}
286+
287+
if bookFound.copies <= 0 {
288+
fmt.Println("no copias disponibles")
289+
return
290+
}
291+
292+
if v, ok := l.loanBooks[userDoi]; ok {
293+
v = append(v, bookTitle)
294+
l.loanBooks[userDoi] = v
295+
} else {
296+
l.loanBooks[userDoi] = []string{bookTitle}
297+
}
298+
bookFound.copies--
299+
fmt.Println("préstamo del libro ", bookTitle, " al usuario ", userDoi, "fué registrado.")
300+
}
301+
302+
func (l *Library) ReturnBook(bookTitle, userDoi string) {
303+
var bookFound *Book
304+
for i, book := range l.books {
305+
if book.title == bookTitle {
306+
bookFound = &l.books[i]
307+
break
308+
}
309+
}
310+
for key, values := range l.loanBooks {
311+
if key == userDoi {
312+
var newList []string
313+
for _, v := range values {
314+
if v != bookTitle {
315+
newList = append(newList, v)
316+
}
317+
}
318+
bookFound.copies++
319+
l.loanBooks[userDoi] = newList
320+
fmt.Println("Libro devuelto correctamente")
321+
break
322+
}
323+
}
324+
}
325+
*/

0 commit comments

Comments
 (0)