source: FarmatikoData/FarmatikoRepo/PHRepo.cs@ dae4cde

Last change on this file since dae4cde was e0cdea2, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 22 months ago

Fix all bugs

  • Property mode set to 100644
File size: 6.2 KB
Line 
1using FarmatikoData.FarmatikoRepoInterfaces;
2using FarmatikoData.Models;
3using Microsoft.EntityFrameworkCore;
4using System.Collections.Generic;
5using System.Linq;
6using System.Threading.Tasks;
7
8namespace FarmatikoData.FarmatikoRepo
9{
10 public class PHRepo : IPHRepo
11 {
12 private readonly FarmatikoDataContext _context;
13 public PHRepo(FarmatikoDataContext context)
14 {
15 _context = context;
16 }
17 //GET
18 public async Task<PharmacyHead> GetPharmacyHeadByIdAsync(int id)
19 {
20 var Phead = await _context.PharmacyHeads.Where(x => x.Id == id).FirstOrDefaultAsync();
21 return Phead;
22 }
23
24 public async Task<IEnumerable<PharmacyHead>> GetPharmacyHeadInfo()
25 {
26 var PHeadInfo = await _context.PharmacyHeads.Take(10)
27 .Where(x => x.DeletedOn == null)
28 .Select(x => new PharmacyHead
29 {
30 Id = x.Id,
31 Name = x.Name,
32 Email = x.Email,
33 Password = x.Password,
34 Pharmacies = x.Pharmacies,
35 Medicines = x.Medicines
36
37 }).ToListAsync();
38 return PHeadInfo;
39 }
40 //POST
41 public async Task UpdatePharmacyHead(PharmacyHead pharmacyHead)
42 {
43 var user = await _context.Users.Where(x => x.Email == pharmacyHead.Email).FirstOrDefaultAsync();
44 var EditedPHead = await _context.PharmacyHeads.Where(x => x.Email.Equals(pharmacyHead.Email)).FirstOrDefaultAsync();
45
46 /*if (!EditedPHead.Email.Equals(pharmacyHead.Email) && !user.Email.Equals(pharmacyHead.Email))
47 {
48 EditedPHead.Email = pharmacyHead.Email;
49 user.Email = pharmacyHead.Email;
50 }*/
51
52 if (!EditedPHead.Name.Equals(pharmacyHead.Name) || !user.Name.Equals(pharmacyHead.Name))
53 {
54 EditedPHead.Name = pharmacyHead.Name;
55 user.Name = pharmacyHead.Name;
56 }
57
58 if (!EditedPHead.Password.Equals(pharmacyHead.Password) || !user.Password.Equals(pharmacyHead.Password))
59 {
60 EditedPHead.Password = pharmacyHead.Password;
61 user.Password = pharmacyHead.Password;
62 }
63 /*if (EditedPHead.Pharmacies != null)
64 {
65 if (EditedPHead.Pharmacies.Count() > 0)
66 if (!EditedPHead.Pharmacies.Equals(pharmacyHead.Pharmacies))
67 EditedPHead.Pharmacies = pharmacyHead.Pharmacies;
68 }
69 if (EditedPHead.Medicines != null)
70 {
71 if (EditedPHead.Medicines.Count() > 0)
72 {
73 if (!EditedPHead.Medicines.Equals(pharmacyHead.Medicines))
74 EditedPHead.Medicines = pharmacyHead.Medicines;
75
76 }
77
78 }*/
79
80 _context.SaveChanges();
81 }
82 public async Task ClaimPharmacy(RequestPharmacyHead pharmacy)
83 {
84 var phead = _context.PharmacyHeads.Where(x => x.Email.Equals(pharmacy.Head.Email)).FirstOrDefault();
85 pharmacy.Head = phead;
86 await _context.PHRequests.AddAsync(pharmacy);
87 await _context.SaveChangesAsync();
88 }
89 public async Task Add(PharmacyHead pharmacyHead)
90 {
91 await _context.PharmacyHeads.AddAsync(pharmacyHead);
92 await _context.SaveChangesAsync();
93 }
94
95 public async Task Remove(PharmacyHead phead)
96 {
97 var Phead = await _context.PharmacyHeads.Where(x => x.Id == phead.Id).FirstOrDefaultAsync();
98 Phead.DeletedOn = phead.DeletedOn;
99 await _context.SaveChangesAsync();
100 }
101
102 public async Task RemoveClaimingRequest(int id)
103 {
104 var req = await _context.PHRequests.Where(r => r.Id == id).FirstOrDefaultAsync();
105 _context.PHRequests.Remove(req);
106 await _context.SaveChangesAsync();
107 }
108
109
110 public PharmacyHead GetPharmacyHeadByUserName(string userName)
111 {
112
113
114 var PHead = _context.PharmacyHeads
115 .Where(x => x.Email.Equals(userName))
116 .Select(x => new PharmacyHead
117 {
118 Email = x.Email,
119 Name = x.Name,
120 Password = x.Password,
121 Medicines = x.Medicines,
122 Pharmacies = x.Pharmacies
123 }).FirstOrDefault();
124
125
126
127 return PHead;
128 }
129
130 public List<PharmacyHeadMedicine> GetPharmacyHeadMedicines(string email)
131 {
132 var Phead = _context.PharmacyHeads.Where(x => x.Email.Equals(email)).FirstOrDefault();
133 var Medicines = _context.PharmacyHeadMedicines.Select(x => new PharmacyHeadMedicine
134 {
135 PheadId = x.PheadId,
136 Head = x.Head,
137 MedicineId = x.MedicineId,
138 Medicine = x.Medicine
139 }).ToList();
140 if (Medicines == null || Medicines == default)
141 Medicines = null;
142 var meds = Medicines.Where(x => x.PheadId == Phead.Id).ToList();
143
144 return meds;
145 }
146
147 public IEnumerable<PharmacyHead> GetPharmacyHeads()
148 {
149 var heads = _context.PharmacyHeads.ToList();
150 return heads;
151 }
152
153 public PharmacyHead GetPharmacyHead(string head)
154 {
155 var phead = _context.PharmacyHeads.Where(x => x.Email.Equals(head)).FirstOrDefault();
156 return phead;
157 }
158
159 public List<Pharmacy> GetPharmacies()
160 {
161 var pharms = _context.Pharmacies.ToList();
162 return pharms;
163 }
164
165 public void DeletePHMedicine(int id, int phId, int medId)
166 {
167 var PH = _context.PharmacyHeadMedicines.Where(x => x.PheadId == phId).Single();
168 var PHMed = _context.PharmacyHeadMedicines.Where(x => x.MedicineId == medId).Single();
169 var PHID = _context.PharmacyHeadMedicines.Where(x => x.Id == id).Single();
170 _context.PharmacyHeadMedicines.Remove(PHID);
171 _context.SaveChanges();
172 }
173 }
174}
Note: See TracBrowser for help on using the repository browser.