1 | using FarmatikoData.FarmatikoRepoInterfaces;
|
---|
2 | using FarmatikoData.Models;
|
---|
3 | using System;
|
---|
4 | using System.Collections.Generic;
|
---|
5 | using System.Linq;
|
---|
6 |
|
---|
7 | namespace FarmatikoData.FarmatikoRepo
|
---|
8 | {
|
---|
9 | public class PharmacyRepository : IPharmacyRepository
|
---|
10 | {
|
---|
11 | private FarmatikoDataContext _context;
|
---|
12 |
|
---|
13 | public PharmacyRepository(FarmatikoDataContext context)
|
---|
14 | {
|
---|
15 | _context = context;
|
---|
16 | }
|
---|
17 |
|
---|
18 | public void Add(Pharmacy pharmacy)
|
---|
19 | {
|
---|
20 | _context.Pharmacies.Add(pharmacy);
|
---|
21 | _context.SaveChanges();
|
---|
22 | }
|
---|
23 | //Just for users
|
---|
24 | public IQueryable<Pharmacy> GetAll()
|
---|
25 | {
|
---|
26 | return _context.Pharmacies.Take(50)
|
---|
27 | .Select(x => new Pharmacy
|
---|
28 | {
|
---|
29 | Name = x.Name,
|
---|
30 | Location = x.Location,
|
---|
31 | Address = x.Address,
|
---|
32 | WorkAllTime = x.WorkAllTime
|
---|
33 | }).OrderBy(x => x.Name);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public ICollection<Pharmacy> GetPharmacies()
|
---|
37 | {
|
---|
38 | return (ICollection<Pharmacy>)_context.Pharmacies.Take(50)
|
---|
39 | .Select(pharmacy => new
|
---|
40 | {
|
---|
41 | pharmacy.Name,
|
---|
42 | pharmacy.Address,
|
---|
43 | pharmacy.Location,
|
---|
44 | pharmacy.WorkAllTime
|
---|
45 | }).OrderBy(x => x.Name);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void Remove(Pharmacy pharmacy)
|
---|
49 | {
|
---|
50 | var pharma = _context.Pharmacies.Where(pharm => pharm.Name.Equals(pharmacy.Name)).FirstOrDefault();
|
---|
51 | if (pharma != null)
|
---|
52 | {
|
---|
53 | _context.Pharmacies.Remove(pharmacy);
|
---|
54 | _context.SaveChangesAsync();
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public void UpdatePharmacy(Pharmacy pharmacy)
|
---|
59 | {
|
---|
60 | var oldPharmacy = _context.Pharmacies.Where(pharma => pharma.Name.Equals(pharmacy.Name)).FirstOrDefault();
|
---|
61 | if (oldPharmacy != null)
|
---|
62 | {
|
---|
63 | _context.Pharmacies.Remove(oldPharmacy);
|
---|
64 | _context.Pharmacies.Add(pharmacy);
|
---|
65 | _context.SaveChangesAsync();
|
---|
66 | }
|
---|
67 | throw new Exception("Pharmacy not found");
|
---|
68 | }
|
---|
69 | }
|
---|
70 | }
|
---|