source: FarmatikoData/FarmatikoRepo/PHRepo.cs

Last change on this file was 8eb1e21, checked in by Dimitar Slezenkovski <dslezenkovski@…>, 4 years ago

Fix delete pharmacy head method.

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